IE and Memory accumulation in Javascript

白昼怎懂夜的黑 提交于 2020-02-17 05:32:04

问题


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


回答1:


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.




回答2:


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.




回答3:


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



来源:https://stackoverflow.com/questions/520680/ie-and-memory-accumulation-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!