Last active
September 14, 2017 07:44
-
-
Save xiongjia/7688374 to your computer and use it in GitHub Desktop.
Dump the NODE V8 heap to a file #devsample #v8
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
/* 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