jQuery.data 会导致内存泄漏吗?
下面的代码会造成内存泄漏吗?
根据 jQuery 文档,使用 data
函数可以避免内存泄漏。确认以下内容是否安全将很有用。
var MyClass = function(el) {
// Store reference of element in object.
this.element = $(el);
};
// Store reference of object in element.
$('#something').data('obj', new MyClass('#something'));
Would the following piece of code create a memory leak.
According to the jQuery documentation use of the data
function avoids memory leaks. It would be useful to confirm whether the following is safe.
var MyClass = function(el) {
// Store reference of element in object.
this.element = $(el);
};
// Store reference of object in element.
$('#something').data('obj', new MyClass('#something'));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
显然,只要 DOM 元素仍然连接到 DOM,现在的代码就会占用额外的内存。但我猜你是在问 DOM 元素不再使用后是否会继续使用额外的内存。
更新:感谢 Joey 的回答(他已删除),我花了一些时间阅读 javascript 中的内存泄漏,看来我在下面段落中的假设是不正确的。因为 DOM 元素不使用纯垃圾回收,所以像这样的循环引用通常会阻止 DOM 元素和 javascript 对象被释放。然而,我相信这个答案的其余部分仍然是正确的。
如果没有深入了解 javascript 引擎如何实现垃圾收集,我无法就这个话题发表权威的言论。然而,我对垃圾收集的一般理解让我认为你的代码是“安全的”,因为从 DOM 中删除#something
元素后,生成的MyClass
object 将仅具有对没有其他连接的对象的引用。垃圾收集器的图形算法应该能够识别 DOM 元素及其MyClass
对象“漂浮在空间中”并且与其他任何东西都没有联系。此外,jQuery 超出了它的范围一旦从 DOM 中删除与给定 DOM 元素关联的数据和事件的方法。来自 文档:
因此,假设您始终如一地使用 jQuery,那么一旦对象从 DOM 中删除,您就只能获得单向引用,这使得垃圾收集器可以
更容易知道它可以删除这些物体。因此,只要在 DOM 元素被删除后没有其他东西引用
MyClass
对象,就不应该出现内存泄漏。Obviously the code as it stands would take up extra memory as long as the DOM element is still connected to the DOM. But I'm guessing you're asking whether it would continue using extra memory after the DOM element is no longer in use.
Update: Thanks to Joey's answer (which he has since deleted), I spent some time reading up on memory leaks in javascript, and it appears my assumptions in the paragraph below are incorrect. Because DOM elements don't use pure garbage collection, a circular reference like this would normally prevent both the DOM element and the javascript object from ever being released. However, I believe the remainder of this answer is still correct.
Without a deep knowledge of how javascript engines implement garbage collection, I can't speak authoritatively on the topic. However, my general understanding of garbage collection makes me think that your code would be "safe" in the sense that after the#something
element is removed from the DOM, the resultingMyClass
object would only have a reference to an object that has no other connections. The graph algorithms of the garbage collector should be able to identify that the DOM element and itsMyClass
object are "floating in space" and unconnected to everything else.Furthermore, jQuery goes out of its way to strip data and events that are associated with a given DOM element once it is removed from the DOM. From the documentation:
So assuming you use jQuery consistently, you would only have a one-way reference once the object is removed from the DOM anyway, which makes it
that much easierpossible for the garbage collector to know it can get rid of these objects.So as long as you don't have something else referencing the
MyClass
object once the DOM element is removed, you shouldn't have a memory leak.我想这取决于 JavaScript 引擎。
您的问题足够精确,可以进行测试。我在对象中添加了一个长字符串,并在一个大循环中运行了潜在的泄漏。
因此,我不认为 IE8 或 Chrome 存在泄漏。
但我也无法重现这些泄漏模式。
I suppose it depends on the Javascritp engine.
You have have the question precisely enought to perform a test. I added a long string in the object and ran the potential leak in a large loop.
As a result, I don't think in leaks in IE8 nor in Chrome.
But I could not reproduce these leakeage patterns either.
这可能会导致内存泄漏。
jQuery.data 方法的理论可能会使用 Data 内部类来缓存 dom 元素的数据。
当然,当您删除缓存数据时,jQuery 将取消引用该数据。
但内部缓存是一个递增数组,当你访问它时,它会继续下去。
所以,最终会出现非常大的缓存数组,这会导致内存泄漏。
在长时间运行的网络应用程序中,这可能会泄漏内存崩溃。
This can lead to a memory leak.
the theory of jQuery.data method may use A Data inner class to cache data for the dom element.
of course,when you remove the cache data,jQuery will unreference the data.
but the inner cache is a increasing array,when you you it ,it will go upon.
so ,in the end,there will be very big cache array,which will lead memeory leak.
In a long run web app,this may leak memory crash.
data 属性仅存储字符串值。
The data attribute only stores string values.