Simple jQuery Ajax call leaks memory in Internet Explorer

后端 未结 8 805
小鲜肉
小鲜肉 2020-12-02 20:33

I created a web page that makes an Ajax call every second. In Internet Explorer 7, it leaks memory badly (20 MB in about 15 minutes).

The program is very simple. It

8条回答
  •  攒了一身酷
    2020-12-02 20:42

    I encountered the same issue and had been stumped all morning ... until a few moments ago. The problem is a circular reference that is created when you set the onreadystatechange handler, that IE isn't smart enough to break. The solution, therefore, is to break it explicitly. However, obviously you can't do it from within the handler itself (though it would be convenient if you could!).

    The magic statement:

    delete request['onreadystatechange'];
    

    You need to keep a record of each XMLHttpRequest object for which you set onreadystatechange. Then, at some point after readyState goes to 4, do your magic on the object. If you are already performing a repeated AJAX poll, the logical place to check for requests to clean up would be in the same polling loop. I defined a simple RequestTracker object to manage my requests.

    This worked for me; I verified that it solved the leak. Here's one link in particular that led the way (I would post more, but StackOverflow isn't letting me):

    • http://bytes.com/topic/javascript/answers/152714-closures-xmlhttprequest-memory-leak

提交回复
热议问题