IE9 memory leak

可紊 提交于 2019-12-07 02:38:33

问题


I'm noticing, in my app, the memory associated with the IE process on Win7 climbs by 20-30 MB with each page refresh. Once I reach about 1.5 GB, the browser becomes unresponsive. I'm using IE9, and can only reproduce this in IE9. No such issue in Chrome, FF, IE7, or IE8. In fact, the issue also does not occur when running IE9 in compatibility mode.

In particular, I'm wondering how memory could leak even after a page refresh. Has anyone else seen this?


回答1:


In the past, Internet Explorer had some problems with references between usual JavaScript variables and DOM objects. So, if I remember correctly, a circular reference like this

var e = document.createElement('div');
var x = { elementReference: e };
e.jsReference = x;

would not be garbage-collected, even if there were no other references to e and x. This is because IE used different methods of garbage collection for DOM elements and JavaScript.

Now, I believed this problem was already remedied in IEs of higher versions, but maybe it wasn't. Try to find all such problematic references and manually remove them if you don't need them anymore.

e.jsReference = null;
x.elementReference = null;

Edit: Test in IE 8

I wrote this simple test webpage.

<html>
  <head>
    <title>Leak test</title>
    <script>
      function leak() {
        var e = document.createElement('div');
        var x = { elementReference: e };
        e.jsReference = x;
      }

      function test() {
        for (var i = 0; i < 10000; i++)
          leak();
        alert('Done');
      }
    </script>
  </head>
  <body>
    <input type="button" value="test" onclick="test();" />
  </body>
</html>

I tested this in IE 8, as I don't have IE 9 installed on this machine. However, this still may be relevant as it shows that the issue was still present even in quite recent versions of IE and thus it may persist even in IE 9.

I opened the page and watched the memory usage. After each pressing of the button, memory usage increased by several MB. After refreshing the webpage, absolutely nothing happened. After closing IE, the memory usage returned to its original state.

You may try that for yourself in IE 9. Of course, you probably aren't allocating 10000 circularily referring objects in your code, but you probably create larger objects that may contain some circular reference you haven't found yet.




回答2:


I'm not sure if this would be your issue, but I was also getting this IE9 memory leak issue where the memory kept growing and growing (about 20mgs per refresh / page change).

If you are using Modernizr (this should be fixed now in one of the very recent releases, 2.5+ I believe), but if you are using an older version (and can't just update it for any reason) then all you need to do is replace one return statement.

The problem is occurring with Modernizr / Geolocation & IE9, it is actually an inherit issue with IE9, not so much Modernizr.

return 'geolocation' in navigator

instead of:

return !!navigator.geolocation // this causes the memory leak (silly IE9)

https://github.com/Modernizr/Modernizr/issues/513

Take a look at that link, but basically the return statement for the Geolocation test needs to be changed, and this problem will fix itself!



来源:https://stackoverflow.com/questions/10606375/ie9-memory-leak

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