卸载网页后Javascript内存泄漏 [英] Javascript memory leaks after unloading a web page

查看:88
本文介绍了卸载网页后Javascript内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在阅读,尝试了解浏览器中的内存泄漏,尤其是。 IE浏览器。我明白漏洞是由Javascript引擎和DOM对象树之间的垃圾收集算法不匹配引起的,并且会持续过去。我不明白的是为什么(根据我正在阅读的文章中的一些陈述),浏览器卸载页面后,内存没有被回收。导航离开网页应该把所有的DOM和JavaScript对象放在范围之外,不应该吗?

I have been reading up to try to make sense of memory leaks in browsers, esp. IE. I understand that the leaks are caused by a mismatch in garbage collection algorithms between the Javascript engine and the DOM object tree, and will persist past. What I don't understand is why (according to some statements in the articles I'm reading) the memory is not reclaimed after the page is unloaded by the browser. Navigating away from a webpage should put all the DOM and javascript objects out of scope at that point, shouldn't it?

推荐答案

这是问题。 IE有一个独立的垃圾收集器为DOM和JavaScript。他们无法检测到两者之间的循环引用。

Here's the problem. IE has a separate garbage collector for the DOM and for javascript. They can't detect circular references between the two.

我们以前用于清除页面卸载时所有节点的所有事件处理程序。然而,这可能会在卸载时停止浏览器。这仅涉及循环引用是由事件处理程序引起的情况。这也可能是由DOM节点添加直接引用引用到DOM节点本身的js对象引起的。

What we used to was to clean up all event handlers from all nodes at page unload. This could, however, halt the browser while unloading. This only addressed the case where the circular reference was caused by event handlers. It could also be caused by adding direct references from DOM nodes to js objects which had a reference to the DOM node itself.

另一件好事是记住,如果你是删除节点,最好先删除处理程序。 Ext-js有一个Ext.destroy方法,这样做(如果你使用ext设置了处理程序)。

Another good thing to remember is that if you are deleting nodes, it's a good idea to remove the handlers yourself first. Ext-js has a Ext.destroy method that does just that (if you've set the handlers using ext).

示例

// Leaky code to wrap HTML elements that allows you to find the custom js object by adding 
//a reference as an "expando" property
function El(node) {
  this.dom = node;
  node.el = this;
}

然后,微软黑客入侵IE,所以在内部卸载时它会删除所有事件处理程序和expando属性因此,它比使用js要快得多。这个修复似乎解决了我们的内存问题,但并不是所有的问题,因为有人仍然有问题。

Then Microsoft hacked IE so it removed all event handlers and expando properties when unloading internally, therefore it's much faster than doing it with js. This fix seemed to fix our memory problems, but not all problems as there are people still having the problem.

MS对问题的描述

MS发布修补程序修复内存泄漏:

有关固定内存泄漏的博客

IE仍然有一些问题

在我们公司,我们使用ext-js。通过总是使用ext-js设置事件处理程序,它具有内部清理例程,我们没有经历内存泄漏。实际上,对于具有4Gb RAM的机器,内存使用量增长但停止在约250Mb。我们不认为这太糟糕了,因为我们加载了大约2Mb(未压缩)的js文件,页面上的所有元素都是动态的。

At our company, we use ext-js. By always setting event handlers using ext-js, which has a an internal clean up routine, we have not experienced memory leaks. In reality, memory usage grows but stops at about 250Mb for a machine with 4Gb of RAM. We don't think that's too bad since we load about 2Mb(uncompressed) of js files and all the elements on the page are dynamic.

有很多话要说关于这一点,我们已经广泛地研究了我在哪里工作。随意问一个更具体的问题。我可以帮助你。

There's a lot to be said about this and we've researched this extensively where I work. Feel free to ask a more specific question. I may be able to help you.

这篇关于卸载网页后Javascript内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆