0

I was trying to find memory leak in my code. I found out when n is, 1 < n < 257 it is showing 0KB consume, but as I put 257 it consumed memory 304KB then increase proportionally with n.

function somefunction()
{

    var n = 256;
    var x  ={};
    for(var i=0; i<n; i++){
        x['some'+i] = {"abc" : ("abc@yxy.com"+i)};
    }


}

// Memory Leak
var init = process.memoryUsage();
somefunction();
var end = process.memoryUsage();
console.log("memory consumed 2nd Call : "+((end.rss-init.rss)/1024)+" KB");
Er. Mohit Agrawal
  • 2,116
  • 1
  • 17
  • 15
  • It doesn't matters what value of n you put. Since x is local variable, memory occupied by it will be freed on the next gc run.Maybe gc cycle didn't executed by the time you recorded memoryUsage in end variable – Anmol Mittal Feb 10 '17 at 13:12

1 Answers1

0

It's probably not leak. You cannot always expect gc to purge everything so soon.

See Why does nodejs have incremental memory usage?

If you want to force garbage collection, see

How to request the Garbage Collector in node.js to run?

Community
  • 1
  • 1
cshu
  • 5,654
  • 28
  • 44