views:

1200

answers:

3

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

A: 

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.

Keith
Thanks for the quick reply, but according to http://blogs.msdn.com/gpde/pages/javascript-memory-leak-detector.aspxthere is no memory leak detected. It seems like the GC is not called often enough. Any suggestions? Thx
GC in javascript is fairly poor - you certainly don't have the sort of control that you'd expect in C# or Java. One of the reasons Chrome and the next FX are so much faster is because they do GC right (at last).
Keith
+9  A: 

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.

tj111
I am not sure what this code did or how is it different but daammmm it worked much better, I tried to open 3 Divs with different ids and memory remains pretty constant,You are one talented person, thank you so much!!!!!!!!!!Suhas
Haha your welcome. The difference being that the element created in the function is not cleaned up by the garbage collection. Whatever happens in the finally statement will execute no matter what. So basically your stuck patching IE's crappy garbage collection.
tj111
Neat trick! I guess you still have to manually destroy the returned el if you store it in the calling code?
meouw
got to say, as I am testing it in a different ways and in many apps, it seems to be working just fine, very very cool, thx for sharing.
+1 - IE has had a javascript garbage-collection bug for at least the last 5 years where is will not release memory until you close the browser - thanks for a workaround!
Steven A. Lowe
Please can you explain why the input parameter and the returning var do have the same name. Is this to confuse beginners (so they can have different names) or do i miss something?
A: 

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

Kevin Tighe