views:

47

answers:

2

The book Learning jQuery says IE has memory leak for the DOM object having a property referencing a function, and the function also referencing the DOM object, thus having a "circular reference", like this:

onload = function() {
    var foo = document.getElementById('foo');
    foo.onclick = function() {  // DOM object foo's onclick property refers to a function
       foo.innerHTML = "hello"  // the function's body refers to the DOM object
    }                           // therefore circular reference
}

IE can handle circular references for garbage collection, but not when the circular references involve both DOM object and Javascript object, because they are handled by different memory managers.

and:

[the memory leak... and] the resulting [reference] looping can't be released by IE even when we navigate away from the page.

never freed until browser is closed.

Is it true? Why does IE not release those memory even when a user leaves the page? Is it because the user may click Back and come back to the page, and IE would like to keep the state of the page? In that case, what if the user is on the memory leak page, and then clicks Back, and then goes to google.com? Then the page is not viewable by any Back or Forward, and the memory leak problem can go away without closing the browser?

Or even when the tab is closed, without closing the browser?

Does this kind of memory leak happen in IE 8 too?

+3  A: 

Memory leaks are a class of program bugs, so you're basically asking "why is IE buggy?". The answer to that is, obviously, "because a programmer, somewhere, made a mistake".

While some browsers intentionally keep the page state even when you navigate away from it (notably Opera and FF), a "memory leak" means memory that the program doesn't use anymore, but forgot to release. In this case, IE has stopped caring about that part of memory, but didn't tell this to the OS (Windows), which still sees it as "used by IE". So this part of the memory hangs in a no-man's land, until the browser is closed - because when the browser process exits, the OS marks all memory allocated to that process as "free".

Memory leaks are a rather insidious type of bug, because the program seems to function correctly, but gradually consumes more and more memory.

See e.g. http://en.wikipedia.org/wiki/Circular_reference and http://en.wikipedia.org/wiki/Garbage_collection_(computer_science) for further reading on this topic.

Piskvor
don't those "circular references" have a "page level" scope? So, when this page is leaking memory and we click `Back` and goes to google.com, that page essentially "disappears" from `Back` and `Forward` sequence, and we can free all memory related to that page.
動靜能量
@Jian Lin: Yes, that's the way it's *supposed* to work. However, a programmer of IE probably forgot to check if all the page references are freed correctly, and so while IE *thinks* it has cleaned up correctly, it has left some data in memory and no longer knows about them.
Piskvor
@Jian: It'd be nice if that worked. The fact that this problem persists though indicates that either the garbage collector thinks that the memory is still referenced properly (though perhaps via an unexpected route) or that the garbage collector is too stupid to reclaim the memory, or even that there is no garbage collector. It's not possible to assess what the problem is without the source to IE (or decompiling it, which would be a faint second best).
Donal Fellows
+1 Good explanation
amelvin
s/programmer/highly paid architect who has retired and bought an island somewhere
jeffamaphone
@jeffamaphone: this kind of bug is usually introduced at a much lower level than "system architecture", I'd say `s/programmer/lowly code monkey/` ;)
Piskvor
No, I don't think so in this case. JScript is owned by DevDiv (the VS guys) and MSHTML is owned by the IE team. I'm pretty sure the issue arises from having two different architectures.
jeffamaphone
+1  A: 

The leak is there because one of the programmers of the application (in this case IE) failed to properly dispose of something (object, resource) that was using memory.

On MSDN the three commonest causes of leaks in managed applications are considered:

  • Holding references to managed objects
  • Failing to release unmanaged resources
  • Failing to dispose Drawing objects
amelvin