It seems that you already understood well how it works, but I will comment on each case.
Simple variables
If there is no further reference to the object, he may be eliminated:
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. In fact, important: the memory is not released at the exact moment when the reference is set to null
. At this point, the value becomes "collectible", but the GC runs at the time the implementation deems it most opportune.
Function
You’re right, if a figure is captured in a closure, he cannot 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
exist.
GIFT
Nowadays there is not much difference when dealing with DOM objects and common objects, The rules explained above apply in the same way. But in the past it was not the case. I remember excluding elements of the DOM without excluding its Event listeners, and later when inserting another node with the same ID as the one that had been removed, the Istener would resurrect! This if I’m not mistaken occurred not only in IE, but also in Firefox. But everything indicates that this kind of thing is really a problem of the past (like the classic circular references problem in the 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 uses an algorithm of the type Mark and Sweep instead of Reference Counting. In Mark and Sweep, the collector traverses all living objects from a root (the global object, in this case), and considers "living" any object that can be referenced in this search. In practice, this means that memory can be released even if there is still some reference to the object to be cleaned, but this reference can no longer be accessed and used by the program. This solved the problem of circular references mentioned above.
I re-read the DOM part from references, and included a section on the types of GC.
– bfavaretto
Why references need to be nullified to be marked as garbage?
– Klaider
@FREEZE Because while there is some reference pointing to a certain object the garbage collector considers that this object is in use, and therefore cannot be removed from memory.
– bfavaretto
But it can’t be any value?
– Klaider
@FREEZE Yes, it can be another value. But usually there is no reason to recycle the variable, and put any value also occupies memory, which is what you want to avoid. Also,
null
traditionally carries the sense of absence of an object.– bfavaretto
Um..., another question, using
WeakMap
can’t tell if an object has been removed from memory?{ let obj = {}; var weak = new WeakMap; weak.set(obj, true); obj = null; }
? When you run this on the console and see the value ofweak
, will probably log the propertyobj
. If you wait a few minutes there won’t be any difference either. Weakmap was supposed to be here.– Klaider
@Phanpy Not quite so, test that your code. If you have questions about Weak maps, post a question here on the site about this :)
– bfavaretto
I’m not sure... `At this point, the value becomes "collectible", but the GC runs at the time the implementation deems it most opportune.` (reference about Weakmap)[https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/WeakMap]
– Klaider