Simple jQuery AJAX leaks in FF4 (Windows7)
My web application has been leaking memory so I created a test webpage to see if I could find out what was actually leaking. So I have arrived at this simple piece of code that can rise to about 300mb of memory usage very quickly.
In Chrome the same code doesnt seem to leak as memory usage drops significantly once GC has run. In FF4 (Windows 7) the memory usage never seems to drop, even once the code has finished running.
What is causing the memory leak here or is it a FF开发者_C百科4 issue (I have noticed a few)?
Note: I am using jQuery 1.5 and am running in Safe Mode with all addons disabled.
$.ajaxSetup ({
cache: false
});
var counter = 0;
ajaxTest();
function ajaxTest()
{
$.ajax({
type: "GET",
url: "/web/data/data.xml",
dataType: "xml",
success: function(xml) {
$("#counter").text(++counter);
xml = null;
if (counter < 2000)
setTimeout(ajaxTest,25);
}});
}
This [anti-]pattern:
function bar()
{
function foo() { setTimeout(bar,25); }
setTimeout(foo,1);
}
is known as "Russian Rulette". On each iteration your create new recursive closure. Technically this is not a leak but just hand-made recursive structure in memory.
you can just call ajaxTest without setTimeout at the end of your callback function. why do you need setTimeout at all?
It could possibly be because cache is set to false. It doesn't really stop your browser from caching responses; it just sends each request uniquely so it won't try to use the cached response. Maybe Chrome deals better with useless cached files... I don't know much about how browser caching works, but two thousand copies of the same xml file probably takes a good deal of memory.
Maybe you could try it with caching enabled to see if the results are different.
EDIT: Actually, that was probably wrong. You should check out http://blog.linkibol.com/2010/05/07/did-you-know-that-jquery-leaks-memory-like-a-fountain/
I'm not quite sure why Chrome would be exempt from the problem though...
精彩评论