diff --git a/src/node_script.cc b/src/node_script.cc index 609b616622..279d26f405 100644 --- a/src/node_script.cc +++ b/src/node_script.cc @@ -355,7 +355,14 @@ Handle WrappedScript::EvalMachine(const Arguments& args) { if (output_flag == returnResult) { result = script->Run(); - if (result.IsEmpty()) return try_catch.ReThrow(); + if (result.IsEmpty()) { + if (context_flag == newContext) { + context->DetachGlobal(); + context->Exit(); + context.Dispose(); + } + return try_catch.ReThrow(); + } } else { WrappedScript *n_script = ObjectWrap::Unwrap(args.Holder()); if (!n_script) { diff --git a/test/pummel/test-vm-memleak.js b/test/pummel/test-vm-memleak.js new file mode 100644 index 0000000000..e2705f48b5 --- /dev/null +++ b/test/pummel/test-vm-memleak.js @@ -0,0 +1,27 @@ +var assert = require('assert'); +var common = require('../common'); + +var start = Date.now(); +var maxMem = 0; + +var interval = setInterval(function() { + try { + require('vm').runInNewContext('throw 1;'); + } catch(e) { + } + + var rss = process.memoryUsage().rss; + maxMem = Math.max(rss, maxMem); + + + if (Date.now() - start > 5*1000) { + // wait 10 seconds. + clearInterval(interval); + } +}, 1); + +process.on('exit', function() { + console.error('max mem: %dmb', Math.round(maxMem / (1024*1024))); + // make sure we stay below 100mb + assert.ok(maxMem < 50 * 1024 * 1024); +});