Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 843
user63362 Avatar asked Feb 06 '09 15:02

user63362


1 Answers

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.

like image 162
TJ L Avatar answered Sep 22 '22 02:09

TJ L