如何销毁JavaScript对象?

时间:2022-03-09 04:14:06

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 overhead and method to stop execution of that process?

我如何看到哪个变量导致内存开销和方法停止该进程的执行?

5 个解决方案

#1


70  

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开发工具来获取应用程序的内存配置文件,以及应用程序中哪些对象需要缩小。

#2


26  

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 references in your objects you may have to de-couple some things.

但是,如果您在对象中创建了循环引用,则可能需要删除一些东西。

#3


15  

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 article on using it and thus also how garbage collection (GC) works in javascript, which is at the core of this question.

它在三年前可能不那么健壮,但Chrome Devevloper Tools的“Profiles”部分现在非常强大,功能丰富。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.

delicateLatticeworkFever适当地提到了循环引用,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.

由于这里提到了删除,所以提供资源理解删除也是很有用的。尽管它没有涉及到任何实际的解决方案,而这些解决方案实际上与js的GC有关。

#4


6  

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将负责其余的工作。

#5


0  

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!

看起来很脏,但实际上它救了我的命!

#1


70  

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开发工具来获取应用程序的内存配置文件,以及应用程序中哪些对象需要缩小。

#2


26  

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 references in your objects you may have to de-couple some things.

但是,如果您在对象中创建了循环引用,则可能需要删除一些东西。

#3


15  

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 article on using it and thus also how garbage collection (GC) works in javascript, which is at the core of this question.

它在三年前可能不那么健壮,但Chrome Devevloper Tools的“Profiles”部分现在非常强大,功能丰富。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.

delicateLatticeworkFever适当地提到了循环引用,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.

由于这里提到了删除,所以提供资源理解删除也是很有用的。尽管它没有涉及到任何实际的解决方案,而这些解决方案实际上与js的GC有关。

#4


6  

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将负责其余的工作。

#5


0  

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!

看起来很脏,但实际上它救了我的命!