When we assign null to a global variable, thereby actively dereferencing and releasing memory, is this a reference counting strategy? Doesn’t it mean that JS’s garbage collection mechanism only uses mark and clear? Does the mark clearing strategy not involve dereferencing?
Variable declaration allocates memory on the heap. Marking the corresponding reference as null tells the interpreter that the memory space corresponding to this variable can be reclaimed.
But this is the syntax design of JS and does not involve the details of memory management in the interpreter implementation. In fact, browser GC strategies are also different. Chrome/Firefox/Safari use mark-and-sweep, while older versions of IE use reference counting.
Also, dereferencing is not only in the form of assigning null. The interpreter can determine the life cycle of variables through scope, and reclaim the memory space of variables when they leave the variable scope.
[Mark Clearance] and [Reference Counting] are two different GC algorithms, while [Dereference] is a grammatical feature in JS. The two can be orthogonal (irrelevant).
The js specification is to use mark and clear, but if implemented, it may not always be mark and clear.
The reason why you are confused here is because you only see the appearance of recycling and fail to see the essence of cleaning.
Reference counting, as the name suggests, counts the references to an object, and when the reference is 0, it is recycled.
Marking and clearing is divided into two phases. The marking phase starts from the root and traverses. Accessible objects are compared to reachable objects, and then in the clearing phase, those objects that are not marked are recycled.
In fact, unless you look at the source code implementation, it is difficult to tell what strategy is used from the surface.