Skip to content

Instantly share code, notes, and snippets.

@jdiamond
Last active August 29, 2015 14:19
Show Gist options
  • Save jdiamond/05d72d509a2831f73665 to your computer and use it in GitHub Desktop.
Save jdiamond/05d72d509a2831f73665 to your computer and use it in GitHub Desktop.
Watch memory and dump heaps in Node.js
function startWatchingMemory() {
if (process.env.MEMWATCH) {
var memwatch = require('memwatch-next');
var heapdump = process.env.HEAPDUMP && require('heapdump');
var hd = null;
memwatch.on('leak', function(info) {
log(info);
if (hd) {
var diff = hd.end();
log(util.inspect(diff, true, null));
}
hd = new memwatch.HeapDiff();
if (process.env.HEAPDUMP) {
var file = 'upload-' + process.pid + '-' + Date.now() + '.heapsnapshot';
heapdump.writeSnapshot(file, function(err) {
if (err) log(err);
});
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment