Reputation: 141
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?
Upvotes: 0
Views: 86
Reputation: 7887
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).
Upvotes: 2