Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Is setInterval handler considered a memory leak?

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.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Is setInterval handler considered a memory leak?

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×