简单的 jQuery Ajax 调用会在 Internet Explorer 中泄漏内存 [英] Simple jQuery Ajax call leaks memory in Internet Explorer

查看:31
本文介绍了简单的 jQuery Ajax 调用会在 Internet Explorer 中泄漏内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个网页,每秒进行一次 Ajax 调用.在 Internet Explorer 7 中,它严重泄漏内存(大约 15 分钟内泄漏 20 MB).

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).

程序非常简单.它只是运行一个进行 Ajax 调用的 JavaScript 函数.服务器返回一个空字符串,JavaScript 代码对它不做任何处理.我使用 setTimeout 每秒运行一次函数,我使用的是 Drip 看东西.

The program is very simple. It just runs a JavaScript function that makes an Ajax call. The server returns an empty string, and the JavaScript code does nothing with it. I use setTimeout to run the function every second, and I'm using Drip to watch the thing.

这是来源:

<html>
  <head>
    <script type="text/javascript" src="http://www.google.com/jsapi"></script>
    <script type="text/javascript">
      google.load('jquery', '1.4.2');
      google.load('jqueryui', '1.7.2');
    </script>
    <script type="text/javascript">
      setTimeout('testJunk()',1000);
      function testJunk() {
        $.ajax({ url: 'http://xxxxxxxxxxxxxx/test', // The url returns an empty string
                 dataType: 'html',
                 success: function(data){}
               });
        setTimeout('testJunk()',1000)
      }
    </script>
  </head>
  <body>
    Why is memory usage going up?
  </body>
</html>

如何堵住这个漏洞?我有一个真正的应用程序,它以这种方式更新一个大表,但如果无人看管,它会占用数千兆字节的内存.

How to plug this leak? I have a real application that updates a large table this way, but left unattended it will eat up gigabytes of memory.

编辑:好的,经过一些好的建议,我将代码修改为:

Edit: okay, so after some good suggestions, I modified the code to:

<html>
  <head>
    <script type="text/javascript" src="http://www.google.com/jsapi"></script>
    <script type="text/javascript">
      google.load('jquery', '1.4.2');
      google.load('jqueryui', '1.7.2');
    </script>
    <script type="text/javascript">
      setTimeout(testJunk,1000);
      function testJunk() {
        $.ajax({ url: 'http://xxxxxxxxxxxxxx/test', // The url returns an empty string
                 dataType: 'html',
                 success: function(data){setTimeout(testJunk,1000)}
               });
      }
    </script>
  </head>
  <body>
    Why is memory usage going up?
  </body>
</html>

不过,这似乎没有任何区别.我没有对 DOM 做任何事情,如果我注释掉 Ajax 调用,内存泄漏就会停止.所以看起来泄漏完全在 Ajax 调用中.jQuery Ajax 是否天生就创建了某种循环引用,如果是这样,我该如何释放它?顺便说一句,它不会在 Firefox 中泄漏.

It didn't seem to make any difference, though. I'm not doing anything with the DOM, and if I comment out the Ajax call, the memory leak stops. So it looks like the leak is entirely in the Ajax call. Does jQuery Ajax inherently create some sort of circular reference, and if so, how can I free it? By the way, it doesn't leak in Firefox.

有人建议在另一个虚拟机中运行测试,看看结果是否相同.我没有设置另一个 VM,而是找到了一台运行 XP Home 和 Internet Explorer 8 的笔记本电脑.它表现出同样的问题.

Someone suggested running the test in another VM and see if the results are the same. Rather than setting up another VM, I found a laptop that was running XP Home with Internet Explorer 8. It exhibits the same problem.

我尝试了一些旧版本的 jQuery 并获得了更好的结果,但直到我放弃了 jQuery 中的 Ajax 并使用更传统(和丑陋)的 Ajax,问题才完全消失.

I tried some older versions of jQuery and got better results, but the problem didn't go away entirely until I abandoned Ajax in jQuery and went with more traditional (and ugly) Ajax.

推荐答案

该问题似乎出在 Internet Explorer 中的 jQuery 1.4 以及 1.2 和 1.3 版的较小程度上.

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 和 1.4.2 都表现出严重的内存泄漏.

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 和 1.3.2 都表现出更小的泄漏(10 分钟后大约 100 KB).

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).

我还尝试了一个以更传统的方式调用 Ajax 的程序版本:

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>

这完全消除了泄漏.

所以看起来我将不得不重复我的重复 Ajax 调用,直到 jQuery 人员解决这个问题.

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.

这篇关于简单的 jQuery Ajax 调用会在 Internet Explorer 中泄漏内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆