简体   繁体   中英

mark and sweep in javascript(context variable)

i am reading Professional JavaScript for Web Developers

i got problem when reading "When the garbage collector runs, it marks all variables stored in memory. It then clears its mark off of variables that are in context and variables that are referenced by in-context variables ."

i know when the object could not be reached by any variables, the memory associated would be reclaimed.

What does "variables that are in context" mean? Are they variables that could be found in the scope chain? But what about the "variables that are referenced by in-context variables"?

i am confused.

I'm assuming it's to avoid accidentally deleting variables used in a closure. In javascript, just like any other functional language, just being unreachable is not enough to tell you weather you should delete an object.

Take for example the following code:

function a () {
    var x=0;
    return function () {
        alert(x++);
    }
}

var b = a();
// at this point, the instance of `x` created by calling `a` is
// no longer reachable but we are still using it in the closure.

If we follow just the "unreachability" rule then the closure created would lose the variable x .

Consider this:

(function(){
    var sobriety = [];
    window.inception = function() {
        var i = 0, 
            j = 0,
            inner_level = { i: i }, 
            level       = { level: inner_level },
            food        = {};
        return function() { 
            var new_level = {
                level: level.level
            };
            new_level[i] = 'step ' + i;
            new_level.level.i = i;
            sobriety[i++] = new_level; 
        };
    };
    window.show_my_sobriety = function() { console.log(sobriety); };
})();

var agent = inception();
agent(); agent(); agent();
show_my_sobriety();​

JS Fiddle .

I admit this example is somewhat sophisticated, but I just had to make it to show the difference between i (a primitive) and inner_level (a reference type).

Here we have a module with one sobriety variable local to it, and two functions made global (by assigning them to properties of window object). Note that these global functions will have access to sobriety variable even after the module which has it defined is finished ( in-context ).

inception function, when invoked, defines five variables: two scalar ( i and j ) and three reference ( inner_level , level and food ), then defines a function and return it.

This function apparently access i and level (the same context), and sobriety (the outer level context) - but not j and food . Hence latter would be collected by GC right after window.inception is complete; the former, though, stay uncollected - because they're referred by the inner functions.

Now the tricky part. While you don't see access for inner_level in this function, it's still accessed - as it's a value of level property of the same-named object. And, when you check the results, you'd see that all three elements have the same level.i value - equal to 2. That's what's understood by "variables that are referenced by in-context variables".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM