Create a gist now

Instantly share code, notes, and snippets.

@xiongjia /dump.js
Last active Sep 14, 2017

What would you like to do?
Dump the NODE V8 heap to a file #devsample #v8
/* Dump the NODE V8 heap to a file
* Usage:
* 1. Install the node-heapdump: npm install node-heapdump
* 2. call below code when you need dump the heap.
* require('./dump.js')('test');
*/
'use strict';
var path = require('path'),
util = require('util');
exports = module.exports = function(name) {
var heapdump, now, dumpFileSuffix, dumpFile;
try {
heapdump = require('heapdump');
}
catch (err) {
util.error('cannot find heapdump');
return;
}
name = name || 'node-heapdump';
now = Date.now();
dumpFileSuffix = util.format('%s-%d-%d.heapsnapshot',
name, process.pid, now);
dumpFile = path.join(__dirname, dumpFileSuffix);
util.log('Begin dump heap to ' + dumpFile);
try {
heapdump.writeSnapshot(dumpFile);
}
catch (err) {
util.log('Err on the dump heap to ' + dumpFile);
return;
}
util.log('End dump heap to ' + dumpFile);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment