

Does JavaScript's setInterval() method cause memory leak? - killahpriest
http://stackoverflow.com/questions/14034107/does-javascript-setinterval-method-cause-memory-leak/

======
ajanuary
From what I can tell the issue stems from the fact that Javascript puts stack
frames on the heap, which means the heap will grow with every function call,
which they're doing every 50 ms.

------
checker
If it was an issue, wouldn't it be engine- or browser-specific?

~~~
ricardobeat
Yes. Firefox had terrible GC pauses until last year [1]. Chrome processes just
a portion of the heap at a time to reduce pauses to a minimum [2].

You can minimize GC by being clever about re-using objects, avoiding waste.
The top answer shows how GC happens less frequently when using a setTimeout
loop instead of setInterval, I don't know the underlying reason but that was
always recommended before requestAnimationFrame came along.

These days you should use requestAnimationFrame whenever available - it allows
the browser to plan ahead for a smoother framerate, adjust rate to the device
capabilities, and save resources when it's in the background.

[1] <http://phoboslab.org/log/2011/08/are-we-fast-yet> [2]
<https://developers.google.com/v8/design#garb_coll>

------
balloot
This is silly. You're creating a new anonymous function on every interval, so
it will increasingly take up memory to hold these anon functions until the GC
kicks in. It's doing exactly what it should do, because you don't want it
doing GC every 50 ms.

~~~
abstractbill
That anonymous function is a _literal_ , just like the number 50 (the next
argument after it). In the same way the 50 doesn't get created each iteration,
neither does the anonymous function.

~~~
balloot
The memory profile says otherwise, doesn't it? If you create a named function
and call it like "setInterval(draw, 50)", the memory use stays constant.

~~~
ricardobeat
Did you get that result on your machine? From all reports in that question the
profile is the same regardless of the anonymous function.

------
malux85
tl;dr : "No"

