Assume I have the following myFunction
& setTimeout
duo
function myFunction(){
var am_i_eaten = 'ffdfjdhsfhs';
setTimeout(function(){
console.log(am_i_eaten);
},3000);
}
myFunction();
Will the setTimeout
keep the scope of myFunction
alive (since it can still print am_i_eaten
without problem), and prevent it from being garbage collected in my Node.JS environment? I believe the behavior to be somewhat different than the behavior in a browser?
Thanks!
Assume I have the following myFunction
& setTimeout
duo
function myFunction(){
var am_i_eaten = 'ffdfjdhsfhs';
setTimeout(function(){
console.log(am_i_eaten);
},3000);
}
myFunction();
Will the setTimeout
keep the scope of myFunction
alive (since it can still print am_i_eaten
without problem), and prevent it from being garbage collected in my Node.JS environment? I believe the behavior to be somewhat different than the behavior in a browser?
Thanks!
Share Improve this question asked Jul 19, 2015 at 15:59 joshjosh 1451 silver badge9 bronze badges 6-
The variables in the closure will be GC'd after the setTimeout has invoked, unless the GC is really smart and you had some other variable which is not referenceable in the timeout's function (i.e. no use of the identifier, no
eval
ing), in which case that other variable may be GC'd. Don't rely on the "smart" bit though, if you're concerned, set the identifier tonull
when you're done with it. – Paul S. Commented Jul 19, 2015 at 16:04 -
Hi, thanks for the response! So by setting the identifier to null, do you mean I should just do
am_i_eaten = null
at the end of function, aftersetTimeout
? – josh Commented Jul 19, 2015 at 16:12 -
no, I mean if you have a second variable which is not used in the timeout's function so isn't necessary for the timeout, e.g.
var foo = ["some", "big", "memory", "hog"];
..after finished with foo,foo = null;
– Paul S. Commented Jul 19, 2015 at 16:17 -
@PaulS. Ah ok, I understand now. What if I don't want the
setTimeout
to keep the function scope alive at all, though? I want to pass whatever variables or references I need to thesetTimeout
, but still garbage collect the function? Could I wrap thesetTimeout
in an anonymous function? – josh Commented Jul 19, 2015 at 16:28 -
To guarantee that behviour the callback needs to be a reference from outside of the function's closure and if you wanted to pass any variables into it then you'd probably need a generator function.
setTimeout
's 3rd+ params are defined as args for the callback but many implementations don't include that functionality (I don't know in node.js). There es a point where you need to ask yourself if the overhead of extra function calls is worth the earlier GCing :) – Paul S. Commented Jul 19, 2015 at 16:40
3 Answers
Reset to default 3What you have created is a function closure and the variables in that closure will not be garbage collected until after the setTimeout()
callback runs.
You can conceptually think of the local variables to a function as individual items that are garbage collected only when no other code that can still be called can reach those variables. So, until after your setTimeout()
callback runs, the variable am_i_eaten
is still reachable and will not be garbage collected.
This works identically in the browser and in node.js (its literally the same V8 JS engine in Chrome and node.js).
setTimeout arbitrary data will be automatically collect by garbage collector once timeout operation done.
In javascript scope of a function is created when the function is created. SetTimeout
takes a callback function which keeps a reference of scope of myFunction
. So it wont be garbage collected till the callback function is called.