Skip to content

Instantly share code, notes, and snippets.

@markbirbeck
Created May 11, 2016 15:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markbirbeck/3de89fbbc19f02392d1e91a3dc8b6c14 to your computer and use it in GitHub Desktop.
Save markbirbeck/3de89fbbc19f02392d1e91a3dc8b6c14 to your computer and use it in GitHub Desktop.
Next time I get a 'memory leak' in Node.... (thanks to http://www.alexkras.com/simple-guide-to-finding-a-javascript-memory-leak-in-node-js/)
var heapdump = require('heapdump');
function generateHeapDumpAndStats(){
//1. Force garbage collection every time this function is called
try {
global.gc();
} catch (e) {
console.log('You must run program with \'node --expose-gc index.js\'');
process.exit();
}
//2. Output Heap stats
var heapUsed = process.memoryUsage().heapUsed;
console.log('Program is using ' + heapUsed + ' bytes of Heap.')
//3. Get Heap dump
process.kill(process.pid, 'SIGUSR2');
}
// setInterval(generateHeapDumpAndStats, 10000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment