Is setInterval handler considered a memory leak?
Problem
The javascript code:
function blah()
{
var blahInterval = setInterval(function(){
if( some_global_variable == 5 )
clearInterval(blahInterval);
}, 1000);
}
I have this code with multiple variants, and it is going to be called gazillion times in a NodeJS script. Shall I delete blahInterval;
after clearInterval
?
Is this a Memory leak? Or the GC will clean it once setInerval function goes out of scope?
Problem courtesy of: ItsMe
Solution
The GC should clean it when it goes out of scope. As long as the if condition can be false to clear the interval. I would not consider it a memory leak.
Solution courtesy of: Spencer Wieczorek
Discussion
View additional discussion.