Skip to content

Instantly share code, notes, and snippets.

@amasad
Last active August 29, 2015 14:26
Show Gist options
  • Save amasad/5d43a5ae98114bd42d49 to your computer and use it in GitHub Desktop.
Save amasad/5d43a5ae98114bd42d49 to your computer and use it in GitHub Desktop.
test heap reclaim
function foo() {
var a = [];
for (var i = 0; i < 20000000; i++) {
a.push({});
}
}
function repeat() {
printMem('before alloc');
foo();
printMem('before gc');
gc();
printMem('after gc');
}
function printMem(label) {
var mem = process.memoryUsage();
for (var p in mem) {
// convert to MB
mem[p] = mem[p] / 1000000;
}
console.log(label, mem);
}
setInterval(function() {
repeat();
}, 1000);
/** SAMPLE RUN
before alloc { rss: 18.518016, heapTotal: 9.751808, heapUsed: 3.452184 }
before gc { rss: 828.45696, heapTotal: 806.798592, heapUsed: 783.449728 }
after gc { rss: 60.657664, heapTotal: 46.335488, heapUsed: 3.395112 }
before alloc { rss: 60.657664, heapTotal: 46.335488, heapUsed: 3.450048 }
before gc { rss: 830.34112, heapTotal: 808.895232, heapUsed: 782.113792 }
after gc { rss: 62.316544, heapTotal: 48.39936, heapUsed: 3.40172 }
before alloc { rss: 62.316544, heapTotal: 48.39936, heapUsed: 3.453744 }
before gc { rss: 830.283776, heapTotal: 808.895232, heapUsed: 782.11828 }
after gc { rss: 62.93504, heapTotal: 48.39936, heapUsed: 3.403368 }
before alloc { rss: 62.93504, heapTotal: 48.39936, heapUsed: 3.453104 }
before gc { rss: 830.283776, heapTotal: 808.895232, heapUsed: 782.11828 }
after gc { rss: 61.886464, heapTotal: 47.367424, heapUsed: 3.403368 }
before alloc { rss: 61.886464, heapTotal: 47.367424, heapUsed: 3.453128 }
before gc { rss: 830.283776, heapTotal: 808.895232, heapUsed: 782.11828 }
after gc { rss: 62.93504, heapTotal: 48.39936, heapUsed: 3.403368 }
before alloc { rss: 62.93504, heapTotal: 48.39936, heapUsed: 3.453104 }
before gc { rss: 830.283776, heapTotal: 808.895232, heapUsed: 782.11828 }
after gc { rss: 61.886464, heapTotal: 47.367424, heapUsed: 3.403368 }
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment