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

I saw in one article that it might be useful to clear all expandos from the window.unload event to prevent memory leaks.

I can't figure out why to do this.

Doesn't the browser clear all DOM and its corresponding resources from it as soon as you leave the page?

Thanks,

burak ozdogan

+2


a source to share


1 answer


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

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

Expando properties in the DOM are nothing more than references to other objects. When the DOM is cleared, it breaks those references, but assumes that the objects are still in use. In this example, the DOM is expecting the script engine to clean up the objects it owns, but the script engine has already exited.



So the problem is that the DOM only cares about the memory it owns and assumes the script engine will do the same.

Hope this helped.

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

+1


a source







All Articles