javascript, circular references and memory leaks

jldupont picture jldupont · Jan 4, 2010 · Viewed 9k times · Source

From what I recall of a not too distant past, Javascript interpreters suffered from memory leaking issues when faced with circular references.

Is it still the case in the latest browsers? (e.g. Chrome, FF 3.5 etc)

Answer

bobince picture bobince · Jan 4, 2010

The vast majority of leaks we talk about with JavaScript are specifically in IE6-7 when you make a reference loop between JavaScript objects and host objects like DOM nodes.

In IE6 this is particularly pernicious in that you don't get the memory back when you leave the page; it's gone until you quit the browser. In IE7 clearing out the page does now return the memory, but you can still have difficulty when you have a long-running application. IE8 solves most of this problem properly by turning the DOM nodes into native JavaScript objects instead of host objects. (You could still trigger the leaks in IE8 by including other non-native objects like ActiveX objects in a reference loop.)

There will certainly still be small obscure memory leaks lurking around in random places for all the browsers, especially in older versions. But there's no one way to easily categorise and avoid them like with the IE refloop issue.