jQuery 1.7.1 Ajax 和内存泄漏

发布于 2024-12-17 19:27:05 字数 987 浏览 0 评论 0原文

当我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

强者自强 2024-12-24 19:27:05

无法判断为什么会发生这种情况,因为没有堆屏幕截图,也没有 done 回调代码,但摆脱所有正在发生的意外关闭将在
至少最大限度地减少内存使用。下面假设它在全局范围内运行/否则为空函数:

var interval;
function setupdaterate(rate) {
    //if the interval wasn't defined only
    if (interval == undefined) {
    interval = setInterval(updateitems, rate * 1000);
    }
}

function updateDone( data ){
    //do the job
}

function iterator() {
    var data = 'ViewObjectId=' + $(this).attr('objectid');

    $.ajax({
        async: true,
        url: '/Ajax/GetUpdatedViewObjectDataHandler.ashx',
        data: data,
        type: 'POST',
        timeout: 10000
        }).done( updateDone
    );
}

function updateitems() {

    $('.updatable').each( iterator );

}

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 at
least minimize the memory usage. The below assumes it is run inside global scope/otherwise empty function:

var interval;
function setupdaterate(rate) {
    //if the interval wasn't defined only
    if (interval == undefined) {
    interval = setInterval(updateitems, rate * 1000);
    }
}

function updateDone( data ){
    //do the job
}

function iterator() {
    var data = 'ViewObjectId=' + $(this).attr('objectid');

    $.ajax({
        async: true,
        url: '/Ajax/GetUpdatedViewObjectDataHandler.ashx',
        data: data,
        type: 'POST',
        timeout: 10000
        }).done( updateDone
    );
}

function updateitems() {

    $('.updatable').each( iterator );

}
蓬勃野心 2024-12-24 19:27:05

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文