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

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

问题描述

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

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

该方案是非常简单的。它只是运行的JavaScript函数,使一个Ajax调用。服务器返回一个空字符串,以及JavaScript code什么都不做吧。我用的setTimeout 运行功能每一秒,和我使用滴灌看的东西。

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>

如何堵塞此泄漏?我有一个更新一个大表这样一个真正的应用程序,但无人理会它会吃掉GB的内存。

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.

修改:好吧,所以在一些很好的建议,我修改了code到:

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的阿贾克斯本身建立某种形式的循环引用,如果是这样,我怎么可以免费呢?顺便说一句,这不漏的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家庭版的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和去与更多的传统(丑)阿贾克斯。

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.

推荐答案

这个问题似乎是与jQuery 1.4在Internet Explorer中,并在较小的程度上,版本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和表现都更小的泄漏(约100 KB的10分钟后)。

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天全站免登陆