-
Notifications
You must be signed in to change notification settings - Fork 7.3k
runInNewContext leaks memory in v0.12.0 #9202
Comments
I've condensed it down to a single script: var vm = require('vm');
var src = vmScript.toString().substr(22);
var script = new vm.Script(src.substr(0, src.length - 2));
var iter = 1e3;
(function runThis() {
script.runInNewContext();
global.gc();
if (0 < --iter)
setImmediate(runThis);
}());
function vmScript() {
var o = {};
var pos = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
function makeid() {
var text = '';
for (var i=0; i < 100; i++)
text += pos.charAt(Math.floor(Math.random() * pos.length));
return text;
}
for (i = 0; i < 1000; i++)
o["p_" +i] = makeid();
} And have confirmed it's an issue. Investigating more. |
This is working in io.js, and I bisected the fix back to the upgrade to V8 3.31.74.1. So I'm not sure if this is a V8 issue, or a Node issue. Will continue to investigate. |
my simplified version looked like: var vm = require('vm');
var text = "var o = {};";
text += "function makeid() { var text = ''; var possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';";
text += "for( var i=0; i < 100; i++ ) text += possible.charAt(Math.floor(Math.random() * possible.length)); return text; }";
//text += " for(i=0;i<1000;i++) o['p_' +i] = makeid();";
var script = new vm.Script(text);
setInterval(function runScript() {
script.runInNewContext();
gc();
}, 10); doing that I watched the @trevnorris the bisect probably comes down to a @saperal could you try applying https://gist.github.com/tjfontaine/5b59ce6c4a0493ed2248 and see if it fixes your problem? |
of course this also breaks other things -- so YMMV :) |
@tjfontaine While I also see that it fixes the memory problem, also consider: ~ContextifyContext() {
context_.Reset();
proxy_global_.Reset();
sandbox_.Reset();
} |
The real fix, and break, comes at |
Ya, I was going through the same thoughts here at work -- basically it feels like there's an overcomplicated nature of how many references we really need to hold on to -- I feel like we should only have to take care of maintaining a weak reference to the v8 context, and it should be responsible for the references to the sandbox and proxy global. I am working to verify that hypothesis now. |
The patch works for the sample app, but breaks other scenarios. Here is a seg fault trace:
|
@trevnorris ... any further thoughts on this one? |
Looks like this may be a duplicate of #6552, which does appear to have been resolved in io.js, we'd just need to decide if it's worth trying to do a backport. |
Memory allocated in script ran under a new context is not freed properly, even as the process runs out of memory and GC becomes aggressive.
In v0.12.0 the application runs out of memory and is stopped by the OS.
In v0.10.36 memory levels off and the issue cannot be reproduced.
I noticed that in node_contextify.cc the function WeakCallback is only called once with
kind == kSandbox
consequently the context is never deleted.To reproduce - run:
node --expose-gc --trace-gc --max_old_space_size=150 index.js
index.js
t.js2
The text was updated successfully, but these errors were encountered: