Does the V8 collect garbage in separate parts of the area?

I am wondering if the V8 does garbage collection in the contents of individual variables within the scope or is it just garbage collection in the entire scope?

So, if I have this code:

function run() {
    "use strict";
    var someBigVar = whatever;
    var cnt = 0;

    var interval = setInterval(function() {
        ++cnt;
        // do some recurring action
        // interval just keeps going
        // no reference to someBigVar in here
    }, 1000);

    someBigVar = somethingElse;
}

run();

Does the V8 collect garbage someBigVar? The closure in run()remains alive due to the callback setInterval(), and obviously the variable cntis still in use, so the whole area run()cannot be garbage collected. But there is no actual permalink to someBigVar.

V8 ? , run() , ? , someBigVar, , , someBigVar?

FYI, V8 ( ).

+4
1

, . , . , , .

- eval . , eval, .

, , weak ( --expose-gc):

var weak = require('weak');
var obj = { val: 42 };

var ref = weak(obj, function() {
  console.log('gc');
});

setInterval(function() {
  // obj.val;
  gc();
}, 100)

ref, gc.

+2

Source: https://habr.com/ru/post/1584569/


All Articles