Last active
August 29, 2015 14:19
-
-
Save jdiamond/05d72d509a2831f73665 to your computer and use it in GitHub Desktop.
Watch memory and dump heaps in Node.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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