AJAX 内存泄漏/膨胀
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我在 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 :(