How does the JavaScript garbage collector work?

14

Simple variables

Correct me if I'm wrong, but in Java the garbage collector deletes objects that are no longer referenced:

Cliente cliente = new Cliente();
cliente = null; // o coletor de lixo cuidará disso

The same goes for JavaScript?

var obj = {foo: "bar"};
obj = null; // isso foi limpo da memória?


Functions

What is the condition for variables within functions to be deleted? I know there are at least some conditions in which they need to be maintained:

var foo = (function() {
    var text = "bar";
    var bar = function() {
        alert(text);
    };

    return bar;
})();

In the above case the variable text must be maintained (I believe), since it is still being referenced by the returned function.


DOM

Removed content is deleted from memory?

<div id="foo">
    <div id="bar">
        ...
    </div>
</div>
var foo = document.getElementById("foo");
foo.innerHTML = ""; // isso será limpo?
    
asked by anonymous 18.09.2014 / 13:36

1 answer

15

It seems that you have understood well how it works, but I will comment on each case.

Simple variables

If there is no reference to the object , it can be deleted:

var obj = {foo: "bar"};
obj = null; // sim, isso foi limpo da memória

However:

var bar = {foo: "bar"};
var boo = bar;
bar = null; // bar não contém mais nada, mas o objeto ainda está em boo

That is, in the case of multiple references to the same object, all need to be nullified so that it is marked as garbage. Incidentally, the memory is not released at the exact moment the reference is set to null . At that point, the value becomes "collectible," but the GC runs when the implementation finds it more timely.

Functions

You are right, if a value is captured in a closure , it can not be released:

var foo = (function() {
    var text = "bar";
    var bar = function() {
        alert(text);
    };

    return bar;
})();

That is, the string "bar" will continue to exist while foo exists.

DOM

Nowadays there is not much difference in dealing with DOM objects and common objects. The rules explained above apply in the same way. But that was not the case in the old days. I remember deleting DOM elements without deleting their event listeners, and later inserting another node with the same ID as it had been removed, the listener was resurrected! This if I was not mistaken happened not only in IE, but also in Firefox. But this sort of thing seems to be a problem in the past (like the classic circular reference problem in IE , which required a series of juggling to be avoided, up to IE8).

Reference Counting vs. Mark and Sweep

According to MDN , the GC of all modern browsers use a type Mark and Sweep instead of Reference Counting . In Mark and Sweep, the collector traverses all live objects from a root (the global object, in this case), and considers any object that can be referenced in that search to be "alive". In practice, this means that memory can be released even though there is still some reference to the object to be cleaned, but that reference no longer has to be accessed and used by the program. This solved the problem of circular references mentioned above.

    
18.09.2014 / 13:47