AJAX 内存泄漏/膨胀

发布于 2024-11-30 11:26:03 字数 1120 浏览 1 评论 0原文

我正在使用 Web 界面开发实时聊天应用程序,并且 FF5(Linux 二进制文件)中的内存占用量不断增加。奇怪的是,Chromium 并没有表现出膨胀。我正在做的事情如下:

1)一个函数启动循环:

function init_chat ()
        {
            doAjax ("my-url", handler_name);
        }

2)doAjax函数:

function doAjax(address, ajax_handler)
        {   
            var xmlhttp;

            if (window.XMLHttpRequest)
            {// code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp=new XMLHttpRequest();
            }
            else
            {// code for IE6, IE5
                xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
            } 

            xmlhttp.onreadystatechange = function() {ajax_handler(xmlhttp);};

            xmlhttp.open("GET", address, true);
            xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            xmlhttp.send();
        }

3)在服务器上,请求线程最多等待3秒以获得新信息,并返回一个JSON响应

4 ) 处理函数处理响应并再次调用 doAjax,并将其自身作为处理函数。

据我了解,这不是真正的递归,因为 ajax 调用应该生成一个新线程,并且处理函数理论上不应将跳转指针保留回 doAjax 函数。也许我正在创建一个闭包,但它没有被正确收集?如果是这样,我该如何避免呢?

提前致谢, 维克.

I'm doing a real-time chat application with a web interface, and I am getting a constantly growing memory footprint in FF5 (Linux binary). Curiously, Chromium doesn't exhibit the bloat. What I'm doing is the following:

1) A function kick-starts the cycle:

function init_chat ()
        {
            doAjax ("my-url", handler_name);
        }

2) The doAjax function:

function doAjax(address, ajax_handler)
        {   
            var xmlhttp;

            if (window.XMLHttpRequest)
            {// code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp=new XMLHttpRequest();
            }
            else
            {// code for IE6, IE5
                xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
            } 

            xmlhttp.onreadystatechange = function() {ajax_handler(xmlhttp);};

            xmlhttp.open("GET", address, true);
            xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            xmlhttp.send();
        }

3) On the server, the request-thread waits at most 3 seconds for new info, and returns a JSON response

4) The handler function processes the response and calls doAjax again, with itself as the handler function.

From what I understand, this isn't true recursion, as the ajax call should spawn a new thread, and the handler function shouldn't theoretically hold a jump pointer back to the doAjax function. Maybe I'm creating a closure and it's not being collected properly? If so, how can I avoid it?

Thanks in advance,
Vic.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

つ低調成傷 2024-12-07 11:26:03

我在 FF 中也看到过类似的情况 - 通常极端的内存膨胀来自像 firebug 这样的插件;不过,我总是建议在 JS 中手动取消对象以强制内存清理。 JS 的内存管理通常很差,最好的做法是自己清理..手动:(

I have seen similar things with FF - usually the extreme memory bloat comes from plugins like firebug; however, I always recommend manually nullifying objects in JS to force memory clearing. Memory management with JS is generally poor, it is best practice to clean up after yourself..manually :(

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