Created
May 11, 2016 15:24
-
-
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/)
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
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