JavaScript memory leak

I just noticed that some kind of javascript that just was written seems to be a memory leak, this is a pretty simple piece of code - thanks to jquery - but I can watch it work in the taskmanager, and memory usage slowly clicks up from 4 up to 40 bytes.

All I do is throw some data into asp mvc controller / action via getJSON:

$(document).ready(function () { var olddata = ""; window.setInterval(function () { var options = JSON.stringify({ orderby: "name" }); var params = { options: options, data: olddata ? JSON.stringify(olddata) : "" }; $.getJSON("/Home/GetTasks", params, function (json) { olddata = json; json = null; }); params = null; options = null; }, 1000); }); 

I typed the timer value only to make it easier to see the problem. Obviously, I am doing something wrong, but I don’t see that.

Should I clear the getJSON call?

TIA.

+7
source share
1 answer

How do you know that you are actually losing your memory?

In small amounts, such as 4 and 40 bytes, you can just see the heap grow, but some of the new blocks in the heap are “free” and available for future use, so when the overall memory usage of the application grows, the memory is not actually a leak and will available for future use, so it will not grow forever.

If this is the entire volume of your experiment, I do not see any problems with the code.

There are three function closures here. Closing $(document).ready() lasts the whole life of your code, but this is just a one-time deal, so there should be no problem.

The anonymous function passed to setInterval() supports closing $(document).ready() . Each call to the anonymous setInterval() function must be a new call that receives a new set of local variables and displays it old when previous calls have completed.

The anonymous function passed to getJSON() creates a closure of the anonymous setInterval function, but this closure should only continue until the getJSON function completes, and when the closure of the anonymous setInterval() function is closed.

The only closure that I see here is the closure of $(document).ready() , which you intend, and it is created only once, so it should not cause leaks.

All local variables in the anonymous getJSON function will be released when it ends. The only data from the getJSON call that survives is your assignment:

 olddata = json; 

But each subsequent assignment simply replaces the data of the previous call, so the previous data is no longer referenced and available for disposal by the garbage collector.

There is no DOM manipulation here, so there is no possibility of cross or circular references between the DOM and JS.

My conclusion is that I do not see anything that leaks out. I see a lot of things using temporary memory, so I suspect that what you see in using the process memory is just heap growth, but growth in such a way that the memory that grew will eventually be reused. If the browser also caches JSON results, you can also see an increase in the memory cache.

Unfortunately, in today's browsers it’s hard to say when it really is a memory leak compared to the temporary memory expansion of the browser used by caching, large heaps, etc. In a pinch, you could set all the caches very small and run this for a long time (hundreds of thousands of iterations). If this is not a leak, memory usage should eventually smooth out. If this is a leak, memory usage should continue to grow relatively linearly.

Disclaimer: One disclaimer is that I assume that the jQuery $.getJSON() function does not leak itself and always ends in such a way that it clears the closure that it creates even if the ajax call fails.

+6
source

All Articles