user63362
user63362

Reputation: 83

IE and Memory accumulation in Javascript

Here is the test URL

http://edventures.com/temp/divtest.php

Procedure:

  1. Close all IE instances.
  2. Open the URL in IE7
  3. Open the task manager, look for memory consumed by IE
  4. Now click on Create button,
  5. Watch the memory it will jump up by about 2K
  6. Now click on Destroy button and the DIV will be destroyed but the memory remains the same.
  7. You can try it repeatedly and memory just adds up.

Is there any way to fix this? Any way to call Garbage collector forcefully without reloading the window?

I am under assumption that when I remove DIV the memory will be freed but does not seem to work that way.

Please let me know any fix to this.

Thanks for your help.

Suhas

Upvotes: 7

Views: 5030

Answers (3)

Kevin Tighe
Kevin Tighe

Reputation: 21161

Have you tried this experiment in other browsers? Firefox's memory consumption is much worse than IE's on my machine...

Upvotes: 0

Keith
Keith

Reputation: 155662

Yeah - IE has some awful memory leaks.

Check out IE Drip - you basically have to design your pages so that they don't do what makes IE leak like this.

This is part of the reason why IE is so loathed.

To avoid IE leaking you have to be very careful with how you add HTML elements to the page, especially tables. Be especially careful with non-HTML 3.2 attributes - IE7 is still basically IE4 and attributes external to the old HTML specs is where it tends to go wrong.

Upvotes: 0

TJ L
TJ L

Reputation: 24452

Here's how to create DOM elements and prevent memory leaks in IE.

function createDOMElement(el) {
  var el = document.createElement(el);

  try {
    return el;
  }
  finally {
    el = null;
  }
}

You can use variations of the try/finally trick to prevent the leaks when doing other DOM operations.

Upvotes: 10

Related Questions