At the earliest, I had a cognition of garbage collection mechanism, and variables referenced globally would not be collected by garbage collection mechanism. Later, I had a vague concept of old generation and new generation, and recently I have a further cognition of the working and principle of browser.

In V8, the heap is divided into the new generation and the old generation, with the new generation storing short-lived objects and the old generation storing long-lived objects. Newborn areas usually only support 1 to 8M capacity, while older areas support much more capacity. V8 uses two different garbage collectors for each of these areas to perform garbage collection more efficiently. Secondary garbage collector, mainly responsible for the new generation of garbage recycling. Main garbage collector, mainly responsible for old generation garbage collection.

The Cenozoic insane is processed using the Scavenge algorithm, which splits the Cenozoic space into two sections, one half object and the other free.

In the garbage collection process, the garbage in the object region is marked first; The garbage collector copies the surviving objects into the free area. It also arranges the objects in an orderly manner, so that the copying process is equivalent to memory defragmentation. After the copying, the free area is free of memory fragmentation. After the replication is complete, the roles of the object area and the free area are reversed, that is, the original object area becomes the free area, and the original free area becomes the object area. This completes the collection of garbage objects, and this role reversal allows both areas to be reused indefinitely in the new generation.

Due to the Scavenge algorithm used in the New generation, live objects need to be copied from the object area to the free area each time a cleanup operation is performed. However, the replication operation requires time cost. If the space of the new area is set too large, each clean up will take too long. Therefore, the space of the new area will be set relatively small for efficiency.

Because the newborn area is small, it is easy to fill the entire area with living objects. To solve this problem, the JavaScript engine adopts the object promotion strategy, where objects that have survived two garbage collections are moved to the old area.

The main garbage collector is mainly responsible for garbage collection in the old area. In addition to the promoted objects in the freshman area, some large objects will be directly assigned to the old area. Therefore, objects in old age area have two characteristics, one is that objects occupy large space, the other is that objects live for a long time. The main garbage collector uses a Mark-sweep algorithm for garbage collection.

Mark the process stages. The marking phase is to start from a set of root elements and recursively traverse this set of root elements. In this traversal process, the elements that can be reached are called active objects, and the elements that cannot be reached can be judged as junk data. Multiple execution of the mark-clear algorithm on a piece of memory results in a large number of discrete memory fragments. Too much fragmentation can cause large objects to be unable to allocate enough contiguous memory, leading to another algorithm called mark-compact. This marking process is still the same as in mark-clean, but the next step is not to clean up the recyclable objects directly. Instead, move all surviving objects to one end and then clean up memory directly beyond the end boundary.

Since JavaScript runs on the main thread, once the garbage collection algorithm is executed, the JavaScript script that is being executed needs to be suspended and script execution is resumed after garbage collection is completed. We call this behavior stop-the-world.

In the V8 new generation garbage collection, due to its small space and fewer surviving objects, total pause has little impact, but the old generation is different. In order to reduce the lag caused by old garbage collection, V8 divided the tagging process into sub-tagging processes, alternating garbage collection tagging and JavaScript application logic until the tagging phase was complete. We called this algorithm Incremental Marking algorithm.

Using incremental marking algorithm, can take a full garbage collection task broken down into many small tasks, these small task execution time is shorter, can with among other JavaScript task execution, so that when performing the above animation effects, will not let the user feel because of the garbage collection task page card.

There are many kinds of garbage collection algorithms, but no one will be suitable for all scenarios. You need to balance various scenarios and use different algorithms according to the lifetime of the object to achieve the best results.

Coding Personal notes public number