The javascript garbage collection mechanism we usually understand is superficial. “It frees the memory of unreferenced variables.” Recently, I read node.js and learned about the V8 garbage collection algorithm in detail.

Knock on the blackboard: Garbage collection algorithm for v8 engines

V8’s garbage collection strategy is mainly based on generational garbage collection mechanism. In modern garbage collection algorithm, garbage collection of memory is divided into different generations according to the lifetime of the object, and then more efficient algorithms are implemented for different generations of memory. In V8, there are two main memory generations: the new generation and the old generation. Objects in the new generation are objects with a short lifetime, and objects in the old generation are objects with a long lifetime or resident memory.

Scavenge algorithm

On a generational basis, garbage is recycled mainly through the Scavenge algorithm, in which the Cheney algorithm is used

Cheney algorithm is a garbage collection algorithm implemented by copying. It divides the heap memory into two parts, each of which is called semispace. Of the two Semispace Spaces, only one is in use and the other is idle. Semispace Spaces that are in use are called From Spaces, and Spaces that are idle are called To Spaces. When we allocate objects, we allocate them first in the From space. When garbage collection begins, live objects in the From space are checked, they are copied To the To space, and space occupied by non-live objects is freed. After the replication is complete, the roles of the From space and To space are swapped. In a nutshell, garbage collection is done by copying live objects between two Semispace Spaces.

Mark-Sweep & Mark-Compact

The Scavenge algorithm is ideally suited To the exploitoring generation, which uses space for time. However, when an object is replicated multiple times, has a long life cycle, or is out of space, the object is allocated To an older generation and requires a new algorithm for recycling.

Mark-sweep doesn’t split memory space in half, so there’s no such thing as wasting half of it. Unlike the Scavenge avenge living objects, Mark-sweep iterates through all objects in the heap and marks living objects during the marking phase, and only unmarked objects are cleared during the subsequent scavenging phase. As you can see,Scavenge only copies living objects and Mark-sweep only cleans dead objects.

After mark-sweep performs a Mark Sweep, the memory space is discontinuous. This fragmentation can cause problems for subsequent memory allocation because it is likely that a large object will need to be allocated, and garbage collection will be triggered ahead of time, which is not necessary. After a Mark-Compact object is marked dead, the living object is moved to one end during the defragmentation process. When the move is complete, the memory outside the boundary is cleared directly

Incremental tag

To avoid inconsistencies between the JavaScript application logic and what the garbage collector sees, each of the three basic garbage collection algorithms needs to pause the application logic until the garbage collection is complete. This behavior is called “total pause.” A long “total pause” garbage collection can cause a significant lag in the user’s experience. Taking 1.5 GB of garbage collection heap memory as an example,V8 takes more than 50 milliseconds to do a small garbage collection and more than a second to do a non-incremental garbage collection. This is the amount of time in garbage collection that causes JavaScript threads to pause, and the performance and responsiveness of your application can plummet with this amount of time.

To reduce the pause time associated with full-heap garbage collection,V8 started with incremental marking, which is to break down the actions that were supposed to be done in one stop into many small “steps,” using JavaScript for each one The application logic executes for a short time, and the garbage collection alternates with the application logic until the tagging phase is complete

Further reading

JavaScript Memory Leak tutorial

Intensive reading of Node.js

Node.js PDF

Mind map download address