Circular References no More

Circular References no More

So the thing I’ve always found to be the biggest PITA about “web app” development is the classic DOM/JavaScript circular reference issue (I’m pretty sure this applies to both Firefox and IE and probably others). The DOM is COM (or XPCOM) based and measures lifetime of objects by reference counting while JavaScript relies on a garbage collector. The problem in the past has been they have no insight into each others’ memory management so when you have an event handler which has a reference to a DOM object, the DOM object holds on to a reference to the handler and the handler holds a reference to the DOM meaning the JavaScript garbage collector can’t clean up since it knows there’s an external reference to the handler and the DOM can’t clean up since it knows there’s a reference to it from JavaScript that the handler’s holding. It can be pretty easy to end up in a situation like this when using closures as event handlers since closures keep a copy of all local variables from the function they are declared in. You wind up in similar situations when interacting with other COM based add ons like Silverlight 1.0 from JavaScript.

When I was looking around for tooling to help identify these leaks (they’re not always easy to find, even through thorough code inspection and best practices like eliminating closures as event handlers), I came across this IE8 whitepaper:

Not sure if the fix includes only DOM object relate leaks or fixes COM references more generally. Will try to find out and post an update if I find an answer.

I’m actually surprised I haven’t heard about this before. Memory leaks due to DOM/JavaScript interaction are one of the biggest headaches of anyone doing web “apps” like Popfly where the leakage really becomes noticeable as people interact with your pages extensively and for long periods of time. I don’t think Firefox did anything about the issue in 3.0 (but they might have, I admit that since I didn’t even know about this in another MS product – IE8, I could very well have missed Firefox fixing the problem as well). IE developer marketing folks – where are you? This is awesome.