Skip to content

Instantly share code, notes, and snippets.

@kpdecker
Created April 17, 2014 00:26
Show Gist options
  • Save kpdecker/10944589 to your computer and use it in GitHub Desktop.
Save kpdecker/10944589 to your computer and use it in GitHub Desktop.
V8 error stack retention
// #! 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);
@kpdecker
Copy link
Author

@geek
Copy link

geek commented May 13, 2014

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment