如何销毁 JavaScript 对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10246215/
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 to destroy a JavaScript object?
提问by Amit Shah
Recently, I came across one of my application which consumes too much memory and increasing by 10 MB/sec.
最近,我遇到了我的一个应用程序,它消耗了太多内存并以 10 MB/秒的速度增加。
So, I like to know the best way to destroy JavaScript object and variables so memory consumption stay down and my FF can't get destroyed.
所以,我想知道销毁 JavaScript 对象和变量的最佳方法,这样内存消耗就不会降低,我的 FF 也不会被销毁。
I am calling two of my JavaScript in every 8 sec interval without reloading the page.
我每隔 8 秒调用两个 JavaScript 而不重新加载页面。
function refresh() {
$('#table_info').remove();
$('#table').hide();
if (refreshTimer) {
clearTimeout(refreshTimer);
refreshTimer = null ;
}
document.getElementById('refresh_topology').disabled=true;
$('<div id="preload_xml"></div>').html('<img src="pic/dataload.gif" alt="loading data" /><h3>Loading Data...</h3>').prependTo($("#td_123"));
$("#topo").hide();
$('#root').remove();
show_topology();
}
How can I see which variable cause Memory overheadand method to stop execution of that process?
如何查看哪个变量导致内存开销和停止执行该进程的方法?
回答by Yochai Akoka
You could put all of your code under one namespace like this:
您可以将所有代码放在一个命名空间下,如下所示:
var namespace = {};
namespace.someClassObj = {};
delete namespace.someClassObj;
Using the delete
keyword will delete the reference to the property, but on the low level the JavaScript garbage collector (GC) will get more information about which objects to be reclaimed.
使用delete
关键字将删除对该属性的引用,但在底层,JavaScript 垃圾收集器 (GC) 将获得有关要回收哪些对象的更多信息。
You could also use Chrome Developer Tools to get a memory profile of your app, and which objects in your app are needing to be scaled down.
您还可以使用 Chrome 开发者工具获取应用的内存配置文件,以及应用中哪些对象需要缩小。
回答by delicateLatticeworkFever
You can't delete objects, they are removed when there are no more references to them. You can delete references with delete
.
你不能删除对象,当不再有对它们的引用时,它们就会被删除。您可以使用 删除引用delete
。
However, if you have created circular referencesin your objects you may have to de-couple some things.
但是,如果您在对象中创建了循环引用,则可能需要解耦一些事情。
回答by Shwaydogg
While the existing answers have given solutions to solve the issue and the second half of the question, they do not provide an answer to the self discovery aspect of the first half of the question that is in bold: "How can I see which variable cause Memory overhead...?"
虽然现有的答案已经给出了解决问题和问题的后半部分的解决方案,但它们并没有对问题前半部分的自我发现方面提供答案,该问题以粗体显示:“我怎样才能看到哪个变量导致内存开销……?”
It may not have been as robust 3 years ago, but the Chrome Devevloper Tools "Profiles" section is now quite powerful and feature rich. The Chrome team has an insightful articleon using it and thus also how garbage collection (GC)works in javascript, which is at the core of this question.
它在 3 年前可能没有那么强大,但 Chrome Devvloper 工具的“配置文件”部分现在非常强大且功能丰富。Chrome 团队有一篇关于如何使用它以及垃圾收集 (GC)在 javascript 中如何工作的有见地的文章,这是这个问题的核心。
Since delete
is basically the root of the currently accepted answer by Yochai Akoka, it's important to remember what delete does. It's irrelevant if not combined with the concepts of how GC works in the next two answers: if there's an existing reference to an object it's not cleaned up. The answers are more correct, but probably not as appreciated because they require more thought than just writing 'delete'. Yes, one possible solution may be to use delete
, but it won't matter if there's another reference to the memory leak.
由于delete
基本上是 Yochai Akoka 当前接受的答案的根源,因此重要的是要记住 delete 的作用。如果不与接下来两个答案中 GC 工作原理的概念相结合,则无关紧要:如果存在对对象的引用,则不会清除它。答案更正确,但可能不那么受欢迎,因为它们需要更多的思考,而不仅仅是写“删除”。是的,一种可能的解决方案可能是使用delete
,但是否有另一个对内存泄漏的引用并不重要。
delicateLatticeworkFever appropriately mentions circular references and the Chrome team documentation can provide much more clarity as well as the tools to verify the cause.
精致的LatticeworkFever 恰当地提到了循环引用,Chrome 团队文档可以提供更清晰的信息以及验证原因的工具。
Since delete
was mentioned here, it also may be useful to provide the resource Understanding Delete. Although it does NOT get into any of the actual solution which is really related to js's GC.
由于delete
在此处提到,因此提供资源Understanding Delete也可能很有用。虽然它没有进入任何与 js 的 GC 真正相关的实际解决方案。
回答by Oleg V. Volkov
Structure your code so that all your temporary objects are located inside closures instead of global namespace / global object properties and go out of scope when you've done with them. GC will take care of the rest.
构造您的代码,以便您的所有临时对象都位于闭包内,而不是位于全局命名空间/全局对象属性内,并且在您完成它们后超出范围。GC 会处理剩下的事情。
回答by Sergio Abreu
I was facing a problem like this, and had the idea of simply changing the innerHTML of the problematic object's children.
我遇到了这样的问题,并且想到了简单地更改有问题对象的子对象的innerHTML。
adiv.innerHTML = "<div...> the original html that js uses </div>";
Seems dirty, but it saved my life, as it works!
看起来很脏,但它救了我的命,因为它有效!