jQuery Ajax内存泄漏


                    

我绝对不是第一个遇到jQuery内存泄漏问题的人,但是我找不到我正在寻找的与我所看到的类似的许多问题的答案。</ p>

首先,泄漏位:</ p>

  $(document).ready(function ivr_grabber(){
var xhr = $ .ajax({
     网址:“ / ivr / inprogress”,
     超时:3000,

     成功:功能(数据){
         $('#open-ivrs')。html(data);
         数据=空;
     },
     完整:function(){
         setTimeout(ivr_grabber,3000);
     }
});
xhr = null;
});
</ code> </ pre>

PHP代码返回一个表,其中包含一些我们的PBX正在呼叫的人。 我已注释掉.html(data)行,但没有区别。 我还删除了setTimeout,然后放回去,没有运气。 我试图取消可能引起此泄漏的所有值,但我无法弄清它的来源。</ p>

似乎每个ajax调用的泄漏速度约为5 KB。 有什么想法吗?</ p>

在Chrome上使用jQuery 1.7.2 </ p>

我一直在进行堆快照,以上都不影响泄漏</ p>
     </ div>

展开原文

原文

I am definitely not the first person to have a jQuery memory leak issue, but I couldn't find the answer I was looking for the many questions similar to this one that I looked at.

First, the leaky bit:

$(document).ready(function ivr_grabber(){
var xhr = $.ajax({
    url: '/ivr/inprogress',
    timeout: 3000,

    success: function(data){
        $('#open-ivrs').html(data);
        data = null;
    },
    complete: function(){
        setTimeout(ivr_grabber, 3000);
    }
});
xhr = null;
});

The PHP code returns a table with some people who are being called by our PBX. I've commented out the .html(data) line, and it's made no difference. I've also removed setTimeout, and then put it back, no luck. I attempted to unset all the values that might be incurring this leak, but I cant figure out where it's coming from.

It appears to be leaking at a rate of about 5 KB per ajax call. Any thoughts?

Using jQuery 1.7.2 on chrome

I've been taking heap snapshots consistently, none of the above have affected the leakiness

2个回答


我发现了内存泄漏问题。 它在:</ p>

  setTimeout();
</ code> </ pre>

我正在调用一个函数名称,该名称将调用该函数,然后设置超时以调用该函数。 有越来越多的超时堆积。 实际上,我需要使用匿名函数,例如:</ p>

  setTimeout(function(){
     some_func();
},5000);
</ code> </ pre>

编辑:这实际上并不能解决jQuery的ajax方法易于产生的微小的固有句柄累积。 我还没有找到解决这个问题的方法。</ p>

编辑2:我遇到的另一个引起内存泄漏的问题是装入事件处理程序。 重新定义事件处理程序,如下所示:</ p>

  $('#something')。click(function(){做事});
</ code> </ pre>

不会删除旧的.click()处理程序,我发现使用.unbind('click')解决了我遇到的许多问题。</ p>
     </ div>

展开原文

原文

I figured out the memory leak issue. It was in:

setTimeout();

I was calling a function name, which calls the function, and then sets a timeout to call the function. There were escalating timeouts piling up. In reality i needed to use an anonymous function, like so:

setTimeout(function(){
    some_func();
}, 5000);

EDIT: This does not actually solve the tiny inherent handle accumulation that jQuery's ajax methods are prone to. I have yet to figure olut a way around this.

EDIT 2: Another issue I encountered that caused memory leaks was mounting event handlers. Redefining an event handler like so:

$('#something').click(function(){ do stuff });

will not remove old .click() handlers, I found that using .unbind('click') solved many of the issues I was having.


对此我不是100%肯定,但是请尝试在document.ready处理程序范围之外声明函数ivr_grabber。 另外,为什么将jqXHR对象设置为null? 据我所知,这什么也做不了,也许会干扰异步请求。</ p>

  $(document).ready(ivr_grabber);
函数ivr_grabber(){}
</ code> </ pre>
     </ div>

展开原文

原文

I'm not 100% sure on this, but try declaring the function ivr_grabber outside the scope of the document.ready handler. Also, why set the jqXHR object to null? This accomplishes nothing to my knowledge and perhaps interferes with the async request.

$(document).ready(ivr_grabber);
function ivr_grabber() {}

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐