jQuery 1.7.1 Ajax 和内存泄漏
当我使用 jQuery 1.4.2 时,我已经遇到了一些问题(http://stackoverflow.com/questions/8158739/ie-memory-leak-and-eval-with-jquery/8176724#8176724)
现在我已经更新了我的jQuery 到版本 1.7.1,每次迭代后内存都会缓慢增加。
这是我的代码:
var interval;
function setupdaterate(rate) {
//if the interval wasn't defined only
if (interval == undefined) {
interval = setInterval(updateitems, rate * 1000);
}
}
function updateitems() {
$('.updatable').each(function () {
var data = 'ViewObjectId=' + $(this).attr('objectid');
$.ajax({
async: true,
url: '/Ajax/GetUpdatedViewObjectDataHandler.ashx',
data: data,
type: 'POST',
timeout: 10000
}).done(function (data) {
//do the job
});
});
}
10 秒后,所有具有“可更新”类的项目都会更新。但由于某种原因,这段代码泄漏了一些内存。
这是使用 jquery ajax 的最佳方式吗?什么可能导致内存泄漏行为?
我怎样才能找出问题出在哪里?有什么建议吗?
I already had some problems when I was using jQuery 1.4.2 (http://stackoverflow.com/questions/8158739/ie-memory-leak-and-eval-with-jquery/8176724#8176724)
Now I've updated my jQuery to version 1.7.1, and I have the memory increasing slowly after each iteration.
This is the code I have:
var interval;
function setupdaterate(rate) {
//if the interval wasn't defined only
if (interval == undefined) {
interval = setInterval(updateitems, rate * 1000);
}
}
function updateitems() {
$('.updatable').each(function () {
var data = 'ViewObjectId=' + $(this).attr('objectid');
$.ajax({
async: true,
url: '/Ajax/GetUpdatedViewObjectDataHandler.ashx',
data: data,
type: 'POST',
timeout: 10000
}).done(function (data) {
//do the job
});
});
}
After 10 seconds all item with the class "updatable" are updated. But for some reason this code leaks some memory.
Is it the best way of using jquery ajax? What could be causing the memory leak behaviour?
How could I figure out where is the problem? Any advice?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
无法判断为什么会发生这种情况,因为没有堆屏幕截图,也没有
done
回调代码,但摆脱所有正在发生的意外关闭将在至少最大限度地减少内存使用。下面假设它在全局范围内运行/否则为空函数:
Cannot tell why it is happening since there is no heap screenshots and no code for
done
callback but getting rid of all the accidental closures that are happening will atleast minimize the memory usage. The below assumes it is run inside global scope/otherwise empty function:
Esailija,
我尝试了你的代码,它正在工作并且使用与我的相同的内存。今天我注意到一些事情,昨天我打开IE8开发者工具进行测试,我几乎可以肯定这是导致内存泄漏的原因。因为今天我在关闭的情况下进行测试,内存使用情况相当稳定。
我也验证了我的代码,并对它进行了一些评估,我认为这可能是问题所在,但不是,问题确实是 IE8 开发人员工具窗口及其蹩脚的内存管理的错误。
Esailija,
I tried your code, and it was working and using the same memory as mine. I notice something today, yesterday I was testing with the IE8 developer tools opened, I'm almost sure it was cause the memory leak. Cause today I'm testing with that closed and the memory usage is quite stable.
I've verified also my code and I have some evals into it, I thought it could be the problem, but no, the problem was realy fault of IE8 developer tools window and the crappy memory management of it.