Memory Leaks in IE8 and IE9 (Fixed in IE10)

In June 2007, Microsoft famously fixed a problem with memory leaks in Internet Explorer 6. IE8 leaks memory worse than IE6 ever did, yet I haven’t been able to find any mention of it. I say worse because:

  1. No closures or circular references are required.
  2. I cannot find a workaround. (Update: See comments from May 9, 2011 for a way to avoid the problem.)

Update: It was fixed in IE10.

Problem statement

The memory used by IE8 to create certain DOM nodes is never freed, even if those DOM nodes are removed from the document (until window.top is unloaded). There are no closures or circular references involved.

The DOM node types that leak are form, button, input, select, textarea, a, img, and object. Most node types don’t leak, such as span, div, p, table, etc.

This problem only occurs in IE8 (and IE9 and IE10 preview). It does not occur in IE6, IE7, Firefox, or Chrome.

In case you missed it: every image element, anchor element, etc. added to a page uses memory that is not reclaimed even if the element is removed from the page.

In practical terms

Think intranet web application rather than Internet web site—that is, something that you may leave running for multiple days. If you have a web page that never reloads as a whole but pulls over many updates from the server (e.g., images and anchors are updated Ajax-style), each update will leak memory. Note that update here means existing DOM nodes are removed and new DOM nodes are created.

Depending on the size and frequency of the updates, IE8 can end up using all available virtual memory, start thrashing within itself, and eventually failures occur; see below for more details.

The user hitting F5 occasionally (if the page can handle it) will unload window.top and free its memory, but there is no apparent programmatic workaround. If the page is in an iframe, unloading the iframe has no effect. It seems that window.top must be unloaded.

In terms of code

First example

Here’s the basic idea of the leak, which is to repeatedly execute the following:

function leak1() {
    var node = document.getElementById("TO_AREA");
    node.innerHTML = "<img>";
    node.innerHTML = "";
    node = null;
}

Notes:

  • You can leave off the second innerHTML assignment, as well as setting the node to null, which I put in just for clarity/emphasis.
  • There’s an initially empty div with an id of TO_AREA in the body.
  • Example innerHTML that doesn’t leak: <span></span>. See the problem statement above for a list of problematic node types.
  • As I mentioned, there are no closures or circular references involved here.

Second example

Here’s a similar piece of code that avoids innerHTML and has the same leak:

function leak2() {
    var node = document.getElementById("FROM_AREA").cloneNode(true);
    node.id = "NEW_AREA";
    document.body.appendChild(node);
    document.body.removeChild(node);
    node = null;
}

Notes:

  • The FROM_AREA contains HTML like in the first example. Still, there are no closures or circular references.

Third example

This is the simplest and most straightforward:

function leak4() {
    var node = document.createElement("IMG");
    document.body.appendChild(node);
    document.body.removeChild(node);
}

Replace IMG with (e.g.) SPAN, and there is no leak.

Other notes

These examples leak in IE8 but not other browsers. I’ve tried IE8 on Windows XP, Windows Vista, Windows Server 2008, Windows Server 2008 R2, and Windows 7. I’ve tried IE in IE7 backwards compatibility mode, as well as every possible variation of quirks and standards mode. Though that’s by no means every variation of everything, it makes me comfortable that it’s not a fluke.

Live example

Here’s a link to an example page that shows the problem highly amplified. What I mean by amplified is that the job of the page is to show the leak, highly exacerbated; it has no other purpose. There are start/stop links on the page, so don’t worry about the page doing something terrible on its own.

http://test.hemiola.com/leak-ie8.html

Characterizing the leak

The memory size (called private working set on Windows 7) of iexplore.exe grows steadily over time, but this doesn’t lead directly to failures. IE memory usage does seem capped, and it does seem to be trying to manage memory from a DHTML perspective within certain constraints.

After it reaches its self-imposed cap, it seems to start thrashing within itself. Operations that could be performed at maybe 100 times per second begin taking over one minute per operation. Eventually, DHTML-style out-of-memory errors can occur.

In closing

I’m very interested in hearing about workarounds to this problem. I will post any meaningful updates as they are uncovered.

Update 2010-03-26

Microsoft made an IE9 preview available on 2010-03-16. It runs my leak test and does not leak. This is very promising. Additional update on 2010-06-24: We are now up to preview #3, and still no leaks.

There is a lot of memory churn in IE9 for the elements that leak in IE8, and there’s no churn at all for elements that don’t leak in IE8. The memory usage pattern in IE9 could be characterized as following a sawtooth pattern (low amplitude, high frequency), but with no long-term growth. I ran the test for an hour with no memory growth in IE9, whereas IE8 grew by about 1 GB in the same time.

Update 2010-09-16

Microsoft released the first beta of IE9 yesterday. The leak is back, at a rate of about 1 GB an hour: the same elements are leaking as before, at about the same rate as before.

Update 2010-10-28

Microsoft released the sixth preview of IE9 today. The leak is still there, same as in the beta.

Update 2010-11-17

Microsoft released the seventh preview of IE9 today. There’s the same churn and sawtooth as in the original three previews, and likewise, there is no leak. This is turning into a nail-biter.

Update 2011-02-10

Microsoft released the IE9 RC today. The leak is back.

Update 2011-03-14

Today is π Day, and Microsoft released IE9. The leak is still there.

Update 2011-04-12

Microsoft released IE10 Platform Preview 1. In terms of leaking, this is very similar to the IE9 preview: no overall leak, but a lot of churn and the sawtooth pattern is back.

Update 2011-06-29

Microsoft released IE10 Platform Preview 2. No leak. I suppose there is no SmartScreen Filter in these preview releases, which might account for the leak being absent from them.

Update 2011-09-13

Microsoft released the IE10 Platform Preview 3 as part of the Windows 8 Developer Preview. The leak is still there if the SmartScreen Filter is off; otherwise (the default), the leak is not there.

Update 2011-11-29

Microsoft released the IE10 Platform Preview 4 for the Windows 8 Developer Preview. The leak is not there, but I suspect the SmartScreen Filter is also not there; it’s hard to tell.

Update 2012-02-29

Microsoft released the IE10 Platform Preview 5 for the Windows 8 Consumer Preview. The leak is not there, and the SmartScreen Filter definitely is. This is the best it’s looked in years.

Update 2012-05-31

Microsoft released the IE10 Platform Preview 6 for the Windows 8 Release Preview. The leak is not there, and the SmartScreen Filter definitely is. This is the best IE has looked in years. And Windows 8 is looking pretty good, too (after going to the desktop and staying there, that is).

Update 2012-11-13

Microsoft released the release preview of IE10 for Windows 7. The leak is back, just as before: with the SmartScreen Filter disabled, the leak is present; with the SmartScreen Filter enabled, the leak is not present.

Update 2013-02-26

Microsoft released IE10 on Windows 7. The leak is gone! With the SmartScreen Filter enabled, the sawtooth pattern is there. With the SmartScreen Filter disabled, it looks even better: no manic memory usage and no memory growth.

Update 2013-06-26

Microsoft released an IE11 Preview with the Windows 8.1 Preview. I haven’t had a chance to look at it.

Update 2013-07-25

Microsoft released the Developer Preview of IE11 for Windows 7. It looks fine.


Posted

in

by

Comments

52 responses to “Memory Leaks in IE8 and IE9 (Fixed in IE10)”

  1. Fabio M. Costa Avatar
    Fabio M. Costa

    Thank you by the way, very detailed post. Very helpful.

  2. Martin Franke Avatar
    Martin Franke

    First off, thanks for the enlightening post!

    Tried it myself because I will have to deal with IE8 mem leaks in single-page apps, eventually. Since this thread doesn’t appear to be dead I wanted to share my results. (Tests done in VMware, if that might matter. SmartScreen Filter is OFF.)

    Win7 64bit:
    IE8 8.0.7601.17514: leaks, 6.5MB -> 86MB after 23000 iterations (no newer IE8 via Windows Update for me)
    IE9 9.0.8112.16421: leaks, 10.2MB -> 88MB after 22000 iterations (no newer IE9 via Windows Update for me)
    IE10 10.0.9200.16618: no real leak, going up from 5.9MB to 6.9MB over ~20000 iterations, then dropping back (garbage collector?), repeat (stopped after 120000 iterations)

    WinXP SP3 32bit:
    IE8 8.0.6001.18702: stable at ~16.5MB -> NO LEAK (ran it 2 times, stopped after 21000 and 45000 iterations, respectively)

    Findings:
    IE8 on WinXP looks like it has been fixed!
    – IE9 performs better than IE8 — it leaks much faster 😉

    1. Martin Franke Avatar
      Martin Franke

      Forgot to mention that I only checked leak1.

    2. Jens-Ingo Farley Avatar
      Jens-Ingo Farley

      I hope the SmartScreen Filter was off in your WinXP case.

      1. Martin Franke Avatar
        Martin Franke

        Yes, SmartScreen Filter was off, I double checked that.

        1. Jens-Ingo Farley Avatar
          Jens-Ingo Farley

          I powered up an old laptop running WinXP x86 SP3 and IE8, got all the latest updates, tried my test, and it leaked for me. I guess there are other variables.

  3. Arivu Avatar
    Arivu

    Thanks for your detailed post!
    I tested with Win7 64 bit IE9 (9.0.20) on various machines, got the memory leak except one where I installed IE10 some time back and uninstalled latter. May be IE10 fixes related to this fix solved this issue in IE9 ?!

  4. […] IE’s ability to cleanup after itself including the following which seems the most promising (http://com.hemiola.com/2009/11/23/memory-leaks-in-ie8/). Unfortunately, no solutions or workarounds thus […]

  5. Nick H Avatar
    Nick H

    I just wanted to say thank you for your documentation on this. There was a bug in our system related to an old hack to account for this depreciated IE issue, took me over a week to find the source/solution and this was key. <3

  6. […] 即使你采取了很多措施,如通过斯-英戈·法利说明你仍然可以得到内存泄漏在IE这里。 […]

  7. […] BWT:尽管微软列出了以上内存泄漏模型,但是,也有人提出,在IE8下,内存泄漏更为严重,甚至没有循环引用和闭包的情况下,也会出现内存泄漏,并对自己的观点进行了详细分析。请参考:http://com.hemiola.com/2009/11/23/memory-leaks-in-ie8/ […]

  8. […] And even if you take a lot of precautions you can still get memory leaks in IE as described by Jens-Ingo Farley here. […]

  9. […] And even if you take a lot of precautions you can still get memory leaks in IE as described by Jens-Ingo Farley here. […]

  10. […] And even if you take a lot of precautions you can still get memory leaks in IE as described by Jens-Ingo Farley here. […]

Leave a Reply to Jens-Ingo Farley Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.