Can this code produce memory leak?

user4176305

Looks like i found memory leak in my code, but i'm not sure, and i don't have so many experience with nodejs memory leaks.

Can someone explain me, can this code produce memory leak?

    var tasks = [];

    // each 10 seconds
    tasks.push(function () { 
            console.log('hello, world!'); 
    });

    // each minute
    while (tasks.length) {
            var task = tasks.shift();
            task();
    }

UPD: Missed while loop in my code, updated now.

My question is, will scope of my anonymous function from array cleared from memory?

Quentin Roy

Well, not a memory leak but you're putting new elements in your array 6 times faster than you are retrieving them. As a result, you will actually be using only one out of 5 pushed functions, and your array will keep growing. If you let it run long enough, you'll end up with a massive array that can never be emptied.

EDIT: After you added the while loop, the array is not growing anymore, and it shouldn't have any memory leak coming from this part of your code. It does not mean there is none in your project. Make sure any value created in your pushed functions can properly be garbage collected (i.e. that you did not keep a reference on it somewhere).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How memory leak can happen in this code

From Dev

How memory leak can happen in this code

From Dev

Memory Leak in SharePoint Code?

From Dev

Where is memory leak in this code?

From Dev

Can not isolate memory leak

From Dev

How to prevent memory leak in code

From Dev

Does this piece of code leak memory?

From Dev

Why this code is not causing memory leak?

From Dev

Is it a memory leak in the following C code?

From Dev

Memory Leak detection in production code

From Dev

Does this Java code leak memory?

From Dev

Will below code cause memory leak?

From Dev

Does this Qt code leak memory?

From Dev

How to prevent memory leak in code

From Dev

Is it a memory leak in the following C code?

From Dev

Can OpenGL VBOs leak memory?

From Dev

Potential memory leak in Java, may this code lead to the memory leak?

From Dev

How can I solve this memory leak? Actually, where is the memory leak?

From Dev

Where is the memory leak in my C++ code and how can I fix it

From Java

Where is the memory leak in my Perl XS code?

From Dev

Would this simple code cause a memory leak?

From Dev

memory leak in objective-c xcode code

From Dev

Unable to spot Memory leak issue in below code

From Dev

Is there possibly a memory leak in the following node js code?

From Dev

Unusual memory leak in queue - code hangs on exit

From Dev

Qt: is there any memory leak in the following code?

From Dev

Clean up of memory leak in unmanaged code

From Dev

Does this delphi code has memory leak?

From Dev

Why/How is my code causing a memory leak?