简体   繁体   English

jquery ajax内存泄漏

[英]jquery ajax memory leak

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. 我绝对不是第一个遇到jQuery内存泄漏问题的人,但我找不到答案,我正在寻找类似于我所看到的这个问题的许多问题。

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. PHP代码返回一个表,其中包含一些由我们的PBX调用的人。 I've commented out the .html(data) line, and it's made no difference. 我已经注释掉.html(数据)行了,它没有任何区别。 I've also removed setTimeout, and then put it back, no luck. 我也删除了setTimeout,然后把它放回去,没有运气。 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. 它似乎以每个ajax呼叫大约5 KB的速率泄漏。 Any thoughts? 有什么想法吗?

Using jQuery 1.7.2 on chrome 在chrome上使用jQuery 1.7.2

I've been taking heap snapshots consistently, none of the above have affected the leakiness 我一直在不断地拍摄堆快照,以上都没有影响泄漏

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. 编辑:这实际上并没有解决jQuery的ajax方法容易产生的微小的固有句柄累积。 I have yet to figure olut a way around this. 我还没有找到解决这个问题的方法。

EDIT 2: Another issue I encountered that caused memory leaks was mounting event handlers. 编辑2:我遇到的另一个导致内存泄漏的问题是挂载事件处理程序。 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. 不会删除旧的.click()处理程序,我发现使用.unbind('click')解决了我遇到的许多问题。

I'm not 100% sure on this, but try declaring the function ivr_grabber outside the scope of the document.ready handler. 我不是100%肯定,但尝试在document.ready处理程序范围之外声明函数ivr_grabber。 Also, why set the jqXHR object to null? 另外,为什么将jqXHR对象设置为null? This accomplishes nothing to my knowledge and perhaps interferes with the async request. 这根本不会实现,也许会干扰异步请求。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM