Simple jQuery Ajax call leaks memory in Internet Explorer

后端 未结 8 784
小鲜肉
小鲜肉 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
    0 讨论(0)
  • 2020-12-02 20:42

    One problem with your code is that if your ajax request start to take a while you will start flooding the browser and server with ajax request you really should wait till the browser gets a return from the server before kicking off the next one.

    function testJunk() {
        $.ajax({ url: 'http://xxxxxxxxxxxxxx/test', // The url returns an empty string
            dataType: 'html',
            complete: function(data){
                setTimeout(testJunk,1000);
            }
        });
    }
    testJunk();
    
    0 讨论(0)
  • 2020-12-02 20:42

    if you're using the setinterval in javascript and don't clear it properly, the timer can start multiple times, causing a stack of calls.

    try something like

    var myVar = setInterval(function() { clear() }, 5000);
    
    function clear() { 
        clearInterval(myVar); 
        GetData("ServiceLibrary","GetCalls",sdata,Complete);
    };
    
    0 讨论(0)
  • 2020-12-02 20:45

    The problem appears to be with jQuery 1.4 in Internet Explorer, and to a lesser extent, versions 1.2 and 1.3.

    1.4.0, 1.4.1, and 1.4.2 all exhibited the severe memory leak.

    1.2.3, 1.2.6, 1.3.0, 1.3.1, and 1.3.2 all exhibited a much smaller leak (about 100 KB after 10 minutes).

    I also tried a version of my program that calls Ajax in a more traditional way:

    <html>
      <head>
        <script language="javascript" type="text/javascript">
          function getHTTPObject() {
            var xmlhttp;
            /*@cc_on
            @if (@_jscript_version >= 5)
              try {
                xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
              } catch (e) {
                try {
                  xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (E) {
                  xmlhttp = false;
                }
              }
            @else
            xmlhttp = false;
            @end @*/
            if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
              try {
                xmlhttp = new XMLHttpRequest();
                if (xmlhttp.overrideMimeType) {
                  xmlhttp.overrideMimeType("text/xml"); 
                }
              } catch (e) {
                xmlhttp = false;
              }
            }
            return xmlhttp;
          }
          var ajaxObject = getHTTPObject();
          setTimeout(testJunk,1000);
          function testJunk() {
            ajaxObject.open('POST', 'http://XXXXXXXXXXXXXXX/delme2', true);
            ajaxObject.onreadystatechange = handleAjaxResponse;
            ajaxObject.send(null);
          }
          function handleAjaxResponse() {
            if (ajaxObject.readyState==4) {
              setTimeout(testJunk,1000);
            }
          }
        </script>
      </head>
      <body>
        <div id="test">Why is memory usage going up?</div>
      </body>
    </html>
    

    This got rid of the leak entirely.

    So it looks like I'll have to do my repeating Ajax calls the ugly old way until the jQuery folks iron out this problem.

    0 讨论(0)
  • 2020-12-02 20:49

    Here's a link to the bug over on jQuery, along with this as a suggested fix for jQuery 1.4.2:

    --- jquery-1.4.2.js     2010-04-08 12:10:20.000000000 -0700
    +++ jquery-1.4.2.js.fixed       2010-04-08 12:10:38.000000000 -0700
    @@ -5219,7 +5219,7 @@
    
                                // Stop memory leaks
                                if ( s.async ) {
    -                                       xhr = null;
    +                                       xhr.onreadystatechange = null; xhr.abort = null; xhr = null;
                                }
                        }
                };
    

    NOTE: This is officially fixed in jQuery 1.4.4, so your best bet is to just upgrade now.

    0 讨论(0)
  • 2020-12-02 20:49

    eval() will eat up memory for sure (eval happens when passing a string to setTimeout to evaluate), don't use it in testing:

    setTimeout('testJunk()',1000);
    

    should be:

    setTimeout(testJunk, 1000);
    

    Also a better use overall would be setInterval() for a repeated operation like you want, try this:

    setInterval(testJunk, 1000);
    
    0 讨论(0)
提交回复
热议问题