Created
April 17, 2014 00:26
-
-
Save kpdecker/10944589 to your computer and use it in GitHub Desktop.
V8 error stack retention
This file contains hidden or 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
// #! node --trace-gc --expose-gc leaky-error.js | |
var heapdump = require('heapdump'); | |
function Root() { | |
this.exec = function() { | |
throw new Error('test'); | |
}; | |
} | |
// Attach to global to avoid preemptive GC of data in this module | |
global.test = new Root(); | |
var errors = global.errors = []; | |
(function() { | |
for (var i = 0; i < 10000; i++) { | |
try { | |
var root = new Root(); | |
root.exec(); | |
} catch (err) { | |
errors.push(err); | |
} | |
} | |
})(); | |
gc(); | |
gc(); | |
gc(); | |
gc(); | |
// Has 10002 Root instances and ~10000 Error instances | |
heapdump.writeSnapshot('leaky-error-1.heapsnapshot'); | |
errors.forEach(function(err) { | |
err.stack; | |
}); | |
setTimeout(function() { | |
gc(); | |
gc(); | |
gc(); | |
gc(); | |
// Has 2 Root instances and ~10000 Error instances | |
heapdump.writeSnapshot('leaky-error-2.heapsnapshot'); | |
}, 1000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice one... it looks like by default 10 frames are captured and it can be configured by setting
stackTraceLimit
If you set this to 0 instead of calling err.stack at that point do the instances still get collected?