Javascript 你如何清除Javascript中的内存?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7248122/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you clear memory in Javascript?
提问by Dave Stein
var Obj = function(){}; var X = new Obj();
var Obj = function(){}; var X = new Obj();
will X = null
properly clear memory?
会X = null
正确清除内存吗?
Also would this be equivalent?
这也是等价的吗?
var Obj = function(){};
var X = {};
X.obj = new Obj();
delete(X.obj);
EDITIt would seem that although deleting X.obj would NOT immediately clear memory, it would help the garbage collection. If I don't delete X.obj, there would still be a pointer to an object and so the GC may not clean it up.
编辑看起来虽然删除 X.obj 不会立即清除内存,但它会帮助垃圾收集。如果我不删除 X.obj,仍然会有一个指向对象的指针,因此 GC 可能不会清理它。
Although I'm picking @delnan's answer, if you're reading this you should def also catch Benubird's article.
尽管我选择了@delnan 的答案,但如果您正在阅读本文,您还应该阅读 Benubird 的文章。
I also notice I accidentally wrote delete(X) originally instead of delete(X.obj) - sorry.
我还注意到我不小心写了 delete(X) 而不是 delete(X.obj) - 抱歉。
采纳答案by Dave Stein
The short answer is that you don't. delete
simply removes a reference (and not in the way you try to use it, see the above link - delete
is one of those language features few people actually understand), nothing more. The implementation clears memory for you, but it's not your business when (and even if, strictly speaking - this is why one shouldn't rely on finalizers in GC'd languages that offer them) it does. Note though:
简短的回答是你没有。delete
只是删除一个引用(而不是您尝试使用它的方式,请参阅上面的链接 -delete
很少有人真正理解的语言功能之一),仅此而已。该实现为您清除内存,但这不是您的事(即使,严格来说 - 这就是为什么人们不应该依赖提供它们的 GC 语言中的终结器)它会这样做。请注意:
- Only objects that can be proven to be unreachable (i.e. no way to access it) to all code can be removed. What keeps references to whom is usually fairly obvious, as least conceptually. You just need to watch out when dealing with lots of closures, as they may capture more variables than you think. Also note that circular references arecleaned up properly.
- There's a bug in old (but sadly still used) IE versions involving garbage collection of JS event handlers and DOM elements. Google (perhaps even SO) should have better material on my memory.
- 只有可以证明所有代码都无法访问(即无法访问它)的对象才能被删除。保持对谁的引用通常是相当明显的,至少在概念上是这样。您只需要在处理大量闭包时注意,因为它们可能捕获比您想象的更多的变量。另请注意,循环引用已正确清理。
- 旧的(但遗憾的是仍在使用)IE 版本中存在一个错误,涉及 JS 事件处理程序和 DOM 元素的垃圾收集。谷歌(甚至可能是 SO)应该有更好的材料来记录我的记忆。
On the plus side, that means you won't get dangling pointer bugs or (save of course the aforementioned pitfalls) memory leaks.
从好的方面来说,这意味着您不会遇到悬空指针错误或(当然除了上述陷阱)内存泄漏。
回答by Benubird
回答by Ed Heal
No - Javascript runs GC when it feels like it.
否 - Javascript 在感觉时运行 GC。
回答by Ryan Ternier
The Delete method only deletes the reference - not the object. Any other references would be left out in the open waiting for the garbage collector.
Delete 方法只删除引用 - 而不是对象。任何其他引用都将被忽略,等待垃圾收集器。
JavaScript has its own GC, and it will run around and clean things up when nothing refers to them anymore.
JavaScript 有自己的 GC,当没有任何东西引用它们时,它会四处跑动并清理它们。
I still think it's a good practice to null objects. Deleteing an object also helps the GC because it will see something dangling, and say "I'm going to eat you because you're all alone (and now some cynical laugh)".
我仍然认为将对象设为 null 是一个好习惯。删除一个对象也有助于 GC,因为它会看到一些悬空的东西,并说“我要吃掉你,因为你一个人(现在有些愤世嫉俗的笑声)”。
You should look at Deleting Objects in JavaScript
你应该看看在 JavaScript中删除对象
Even though there's a GC, you still want to ensure your script is optimized for performance as peoples computers, browsers, and fricken toolbars (and the number of them), will vary.
即使有 GC,您仍然希望确保您的脚本针对性能进行了优化,因为人们的计算机、浏览器和损坏的工具栏(以及它们的数量)会有所不同。
回答by Chris Baker
Generally speaking, memory management in Javascript is user-agent-specific. The basics of the garbage collector are through reference-counting. So, by setting a reference to null (using the delete
keyword or by explicit assignment), you can assure yourself that a reference will be cleaned up, IFthe object does not have any references that will live outside of its creation scope. That being the case, the GC will have already cleaned up any objects or variables whose scope has ended without your explicitly setting it to null.
一般来说,Javascript 中的内存管理是特定于用户代理的。垃圾收集器的基础是通过引用计数。因此,通过将引用设置为 null(使用delete
关键字或通过显式赋值),您可以确保引用将被清除,如果该对象没有任何位于其创建范围之外的引用。在这种情况下,GC 将已经清除其范围已结束的任何对象或变量,而无需您将其显式设置为 null。
There are some things to take care of, though - circular references are easy to create in JS, especially between a DOM element and an object. Care must be taken to clear (or not create in the first place) references to and/or from DOM elements within objects. If you do create a to/from reference related to DOM, be sure to explicitly clean them up by setting the references to null - both on your object and on the DOM element. Simply setting a parent object to null is not sufficient if there are child objects with references to/from DOM or localStorage because those references will live on, and if there was any reference from the child to the parent, then the parent will live on in memory because of that reference.
但是,有一些事情需要注意 - 在 JS 中很容易创建循环引用,尤其是在 DOM 元素和对象之间。必须小心清除(或首先不创建)对对象内 DOM 元素的引用和/或来自 DOM 元素的引用。如果您确实创建了与 DOM 相关的 to/from 引用,请确保通过将引用设置为 null 来明确清理它们 - 无论是在您的对象上还是在 DOM 元素上。如果有引用到/来自 DOM 或 localStorage 的子对象,那么简单地将父对象设置为 null 是不够的,因为这些引用将继续存在,并且如果从子对象到父对象有任何引用,那么父对象将继续存在内存因为那个引用。
Web pages can actually leak trash in your memory this way - after you navigate away, the circular references keep objects and DOM elements in memory until you've restarted the browser!
网页实际上可以通过这种方式在您的内存中泄漏垃圾 - 在您离开后,循环引用将对象和 DOM 元素保留在内存中,直到您重新启动浏览器!
An article on the subject: http://docstore.mik.ua/orelly/webprog/jscript/ch11_03.htm, and another detailed look: http://blogs.msdn.com/b/ericlippert/archive/2003/09/17/53038.aspx
一篇关于这个主题的文章:http: //docstore.mik.ua/orelly/webprog/jscript/ch11_03.htm,另一个详细的看:http: //blogs.msdn.com/b/ericlippert/archive/2003/09 /17/53038.aspx
回答by yosh kemu
JavaScript memory is generally handled similarly to Java - I mean there is (or there should be) a garbage collector which would delete the object if there is no references to it. So yes, simply "nullifying " the reference is the only way you should "handle" freeing memory, and the real freeing is the JS host part.
JavaScript 内存的处理方式通常与 Java 类似 - 我的意思是有(或应该有)一个垃圾收集器,如果没有对它的引用,它将删除该对象。所以是的,简单地“取消”引用是您应该“处理”释放内存的唯一方法,而真正的释放是 JS 主机部分。