javascript - Does V8 do garbage collection on individual pieces of a scope? -
i'm interested in whether v8 garbage collection on contents of individual variables within scope or whether garbage collection on entire scope?
so, if have code:
function run() { "use strict"; var somebigvar = whatever; var cnt = 0; var interval = setinterval(function() { ++cnt; // recurring action // interval keeps going // no reference somebigvar in here }, 1000); somebigvar = somethingelse; } run(); will v8 garbage collect somebigvar? closure in run() remains alive because of setinterval() callback , cnt variable still being used whole scope of run() cannot garbage collected. but, there no actual ongoing reference somebigvar.
does v8 garbage collect entire scope @ time? so, scope of run() can't garbage collected until interval stopped? or smart enough garbage collect somebigvar because can see there no code in interval callback references somebigvar?
fyi, here's interesting overview article on v8 garbage collection (it not address specific question).
yes, does. variables used inside of closure retained. otherwise closure had capture defined in outer scope, lot.
the exception if use eval inside of closure. since there no way statically determine, referenced argument of eval, engines have retain everything.
here simple experiment demonstrate behaviour using weak module (run --expose-gc flag):
var weak = require('weak'); var obj = { val: 42 }; var ref = weak(obj, function() { console.log('gc'); }); setinterval(function() { // obj.val; gc(); }, 100) if there no reference ref inside of closure see gc printed.
Comments
Post a Comment