简体   繁体   English

jQuery.remove(),分离DOM元素,但我仍然可以从代码中访问元素。 如何避免泄漏?

[英]jQuery.remove(), detach DOM elements, but I still can access the elements from code. How to avoid leaks?

I'm aware that it's not easy to correctly manage memory inside an application with lot of UI elements and completely based on Ajax (in my app the pages are never reloaded). 我知道在具有大量UI元素的应用程序中正确管理内存并且完全基于Ajax并不容易(在我的应用程序中,页面永远不会重新加载)。 But I would like to understand the following behaviour: 但我想了解以下行为:

I have a root element to which a single child element at a time must be attached (think it as the root element being the app container and the childs the single pages). 我有一个根元素,一次只能附加一个子元素(将其视为根元素是app容器,子元素是单个页面)。 Whenever I switch between child contents, I remove the previous content with jQuery.remove(), but I see that the content is actually detached from the DOM but it remains in memory. 每当我在子内容之间切换时,我都会使用jQuery.remove()删除以前的内容,但我发现内容实际上是从DOM中分离出来的,但它仍保留在内存中。

  1. root and two child contents (child1 and child2) root和两个子内容(child1和child2)
  2. from child1 I switch to child2, asking my app manager to remove child1 before attaching child2 从child1我切换到child2,要求我的应用程序管理员在附加child2之前删除child1
  3. child2 is being attached (I can see it) but I still can use child1 elements from the code that manages child1 child2正在附加(我可以看到它)但我仍然可以使用管理child1的代码中的child1元素

child1 code (which holds references to child1 DOM): child1代码(包含对child1 DOM的引用):

function testaccess(){
   load_and_remove(child2);
   var child1DOM = get_this_dom();
}

child1DOM is still there, and I can manipulate it as if it was still attached to the DOM. child1DOM仍然存在,我可以操作它,好像它仍然附加到DOM。

Ok, I suppose that jQuery.remove() and the GC won't be able to release memory until I have code that will access it, but even if I don't call get_this_dom(), even after exiting testaccess(), I see that FF memory doesn't decrease... 好吧,我想jQuery.remove()和GC将无法释放内存,直到我有代码可以访问它,但即使我没有调用get_this_dom(),即使退出testaccess(),我看到FF内存不会减少......

I wonder how to make GC release all the memory, when I exit child1. 当我退出child1时,我想知道如何让GC释放所有内存。

It will not be removed from the DOM until all references to it are released. 在释放对它的所有引用之前,它不会从DOM中删除。

You should attempt to remove all circular references between the JS DOM and render DOM - they both have separate garbage collectors and work separately. 您应该尝试删除JS DOM和渲染DOM之间的所有循环引用 - 它们都有单独的垃圾收集器并单独工作。 Hence why the mark and sweep JS garbage collector does not catch those. 因此,为什么标记和扫描JS垃圾收集器不能捕获它们。

You could try to rework your code to break the circular reference: 您可以尝试重新编写代码以打破循环引用:

var mything = something();
mything = null;

Here are a couple of articles that might help: 以下是一些可能有用的文章:

http://msdn.microsoft.com/en-us/library/Bb250448 http://msdn.microsoft.com/en-us/library/Bb250448

http://www.javascriptkit.com/javatutors/closuresleak/index.shtml http://www.javascriptkit.com/javatutors/closuresleak/index.shtml

http://javascript.info/tutorial/memory-leaks http://javascript.info/tutorial/memory-leaks

I am pretty sure you could find some more quite quickly regarding this. 我很确定你可以很快找到一些这方面。

Also, you could try the .empty() to release all the child nodes but it calls .remove() to do a bit of work. 此外,您可以尝试.empty()来释放所有子节点,但它调用.remove()来完成一些工作。

Note that some of these issues were fixed in newer versions of jQuery ie 1.5 is better than 1.4 for instance. 请注意,其中一些问题已在较新版本的jQuery中修复,例如1.5优于1.4。

Another post on SO on this: jQuery memory leak with DOM removal 关于SO的另一篇文章: jQuery内存泄漏与DOM删除

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM