In the programming world, memory allocation happens all the time, and at the bottom, it can be divided into three categories

Mark clearance

The algorithm is divided into two phases. The first phase is the tag, starting from a GC root collection, and finding all live objects along the “pointer”

In the cleanup phase, you simply delete the unmarked objects to free up space.

The mark-sweep algorithm is so simple and easy to implement that garbage collection algorithms are a continuation of this idea today.

The disadvantages of the marked elimination method

But the mark-clear algorithm also has two fatal drawbacks: slow allocation and fragmentation.

Replication algorithm

The replication algorithm needs To divide the entire space equally into two parts. First, the From space is allocated. When the From space is full, the GC copies the live object To the To space

The first step of the replication algorithm is also markup, but once the live object is found, it is copied directly to the other half of the space, so GC can be completed in a relatively short time.

And each time the objects are copied, they are concentrated to avoid fragmentation.

Disadvantages of replication algorithm

The advantages and disadvantages of mark-clear algorithm and copy algorithm are very obvious.

Reference counting

Disadvantages of reference counting

One disadvantage of reference counting is that it does not recycle “circular references”

Collection rules (most objects have a short lifetime and become garbage soon after birth)

The object is then divided into generations, and different garbage collection algorithms are implemented for different generations

Using a garbage collection algorithm adds extra overhead to the interpreter compiler

GC frees the programmer from memory management, there is no free lunch, and the price is the extra overhead of the virtual machine/interpreter layer.

GC

GC stands for Garbage Collection

Js recycling strategy

In JavaScript memory management, there is a concept called 'reachable', which is that values that are accessible or available in some way are guaranteed to be stored in memory and reclaimed if they are notCopy the code

As for how to recycle, which is actually how to find these unreachable objects (garbage) and clean them up, the principle of JavaScript garbage collection mechanism is to periodically find the memory (variables) that are no longer used, and then release the memory

You might also wonder why not find unwanted memory and free it in real time? It’s very simple. Real-time overhead is too high

V8 optimization for GC

Generational garbage collection

Above rubbish algorithm in each time the garbage collection will check all the objects in the memory, so for some big, old, survival time long object with new, small, short survival time of object a frequency of the check is very bad, because the former needs long time and don’t need to clean frequently, the latter the contrary, how to optimize it??? So here comes the substitution

The old new generation

V8’s garbage collection strategy is mainly based on the generational garbage collection mechanism. V8 divides the heap memory into the new generation and the old generation, and adopts different garbage collectors, that is, different strategies to manage garbage collection

The objects of the new generation are the objects that have a short lifetime. In short, they are newly generated and usually support only 1 to 8 meters of capacity. The objects of the old generation are the objects that have a long lifetime or are resident in memory

New generation waste recycling

Do you really understand garbage collection