浏览器在不停地吃内存的AJAX +的setInterval [英] Browsers keep eating memory with AJAX + setInterval

查看:674
本文介绍了浏览器在不停地吃内存的AJAX +的setInterval的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在给定的时间间隔使用JavaScript中更新了大量的数据。问题是,不管是什么JS库我使用(甚至是裸骨JS),所有浏览器似乎对每一个AJAX请求分配内存,并不能释放它之后。这里是一个示例剪断,应该重现错误:

I need to update a lot of data within a given interval with JavaScript. The problem is, no matter of what JS library i use (even bare-bone js), that all browsers seem to allocate memory on every AJAX request and are not able to free it afterwards. Here is a sample snipped that should reproduce the error:

    <!DOCTYPE html>
    <html lang="en">
        <head>
            <title>Memleak Test</title>
            <meta charset="utf-8" />
            <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
            <script type="text/javascript">

                function readData() {
                    $.getJSON('data.php');
                }

                $(document).ready(function() {
                    setInterval(readData, 1000);
                });
            </script>
        </head>
        <body>
            <div id="content"></div>
        </body>
    </html>

这是相当于测试页面,请 jsbin

An equivalent test page is available at jsbin

下面是详细信息上这样的:

Here is more info on this:

  • 我也试过把READDATA()函数直接在setInterval()调用闭包。这似乎没有任何区别。
  • 在我使用jQuery在这里,但是任何其他库将产生同样的错误。
  • 在我的data.php脚本只是产生一个假的JSON对象与json_en code()在PHP。
  • 我知道有第二个是一个短的时间内这里,在我制作的脚本的时限为30秒。我只是想看看效果更快(在生产应用程序需要时间,但随后的内存已满太)。
  • 在这里的问题是,该应用程序将全天24小时开放。

这看起来很简单,我认为我做的东西真的错在这里,这将是巨大的,如果一些JS大师在这里可以帮助我出去!

It seems so simple that I think I'm doing something really wrong here, it would be great if some of the JS gurus in here can help me out!

推荐答案

一个可能出现的问题同样张贴的是,如果在XHR请求采取比轮询周期较长(平均)将会有越来越多的队列待处理的请求。如果Web服务器本身开始积压请求,这个可以变成一个恶性循环。

One possible problem with the same posted is that if the XHR requests take longer than the poll period (on average) there will be an increasing queue of pending requests. If the web-server itself starts to backlog requests this can turn into a vicious cycle.

要避免这种可能的情况下,使用CPS风格的编码,其中使用适当的回调做下一个动作。也就是说,没有,直到需要启动下一个请求的(previous请求完成:成功或失败) - 这种方法仍然可以被用来创建一个手动请求队列具有可控的大小,如果众多优秀的请求是必需的。

To avoid this possible case, use a CPS-style coding where the next action is done using the appropriate callbacks. That is, don't start the next request until required (previous request complete: success or failure) -- this approach can still be used to create a manual request queue with a controllable size if numerous outstanding requests are required.

此外,确保未使用的对象的的资格填海工程,因为这是在GC语言内存泄漏的标准的原因。

Also, make sure that unused objects are be eligible for reclamation as this is the "standard" cause of a "memory leak" in a GC language.

快乐编码。

的code在帖子中没有​​任何本质上会泄漏内存。它可能是一个与jQuery内部的问题,但是这仅仅是猜测。此外,像Firebug的工具,监视XHR / Web请求可以的消耗显著数量的内存的所以它的东西,检查并确保该行为是不是海森堡。

The code in the post contains nothing that will inherently leak memory. It could possibly be an issue internally with jQuery, but this is just speculation. Additionally, tools like Firebug that monitor XHR/web requests can consume significant amounts of memory so it's something to check and make sure the behavior is not a Heisenberg.

此外,请记住,增加内存的使用并不表示没有内存泄漏,除非它的增长无限。垃圾收集循环将在主机上感觉它才会发生。

Also, remember that increasing memory usage doesn't not indicate a memory leak unless it grows unbounded. A garbage collection cycle will only occur when the hosts feels like it.

这篇关于浏览器在不停地吃内存的AJAX +的setInterval的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆