开发者

Do I have to clean custom properties (Expandos) on window.onunload event?

In one article I have seen that it may be good to clear all expandos on window.unload event to prevent memory leaks.

I cannot understand why to do this开发者_JAVA百科.

Isn't the browser cleaning all the DOM and its relevant resources of it once you leave the page anyway?

Thanks,

burak ozdogan


Hey, great question. The problem is with circular references between JavaScript objects and DOM nodes.

Let's say you have a global JavaScript object which points to a DOM node and the node has an expando property back to the object. When the page unloads, the script engine "nulls-out" the JavaScript object so it no longer points to the DOM node. But, it cannot release the object from memory because there is still a reference to it (from the DOM). Then the script engine terminates.

Expando properties on the DOM are nothing but references to other objects. When the DOM is being cleaned up, it breaks those references but assumes that the objects are still being used. In this example, the DOM waits for the script engine to clean up the objects that belong to it, but the script engine has already terminated.

So, the problem is that the DOM only takes care of the memory that belongs to it and assumes the script engine will do the same.

I hope this helped.

See: http://msdn.microsoft.com/en-us/library/bb250448%28VS.85%29.aspx

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜