Generational Garbage Collection
The objects in the memory are divided into generations, and the large, old, and long-lived objects are classified as the old generation, and the small, new, and short-lived objects are classified as the new generation. The new generation and the old generation use different garbage Collection Frequency and Garbage Collection Policy
Old and new generations
Objects in the new generation are objects with a short survival time. In simple terms, they are newly generated objects, which usually only 1~8M
support capacity. Objects in the old generation are objects with long survival events or resident memory. The objects that survive the garbage collection in the new generation usually have a relatively large capacity
New Generation Garbage Collection
The new generation objects are garbage collected through an algorithm Scavenge
called . Scavenge算法
In the specific implementation of , a replication method is mainly used, that is, Cheney算法
the Cheney算法
heap memory is divided into two, and one is the space in use, which we call Because 使用区
, one is the idle space we call空闲区
two stages
-
marking stage
Mark all active objects in the used area, copy a copy to the free area and sort after marking (to prevent memory fragmentation)
-
cleaning phase
Clear all objects in the used area, clean up inactive objects in the free area, swap roles between the free area and the used area, and thus cycle
When an object still survives after multiple copies, it will be considered as an object with a long life cycle, and then it will 晋升
be moved to the old generation and managed by the old generation strategy
In addition, if an object is copied to the free area, and the space in the free area exceeds 25%, then the object will be directly transferred 晋升
to the old generation space. The reason for setting the ratio to 25% is that when Scavenge
the recycling , the free area will be turned into the used area, and the allocation of object memory will continue. If the proportion is too large, it will affect the subsequent memory allocation
Old Generation Garbage Collection
The garbage collection of the old generation adopts a simple mark-and-clear method, and uses 标记整理算法
to optimize the memory space to avoid generating memory fragments
The Mark-Compact algorithm can effectively solve it. Its marking phase is no different from the mark-clearing algorithm, but after the mark is over, the mark-compact algorithm will move the living objects (that is, objects that do not need to be cleaned) to the memory. move at one end
Optimization Strategy
- parallel collection
- Incremental Marking and Lazy Cleanup
- concurrent collection
1. Parallel recycling (Parallel)
Before introducing parallelism, we must first understand a concept 全停顿(Stop-The-World)
. JavaScript
It is a single-threaded language. It runs on the main thread. It will block the execution of JavaScript
the script . You need to wait for the garbage collection to complete before resuming the script. implementation, this behavior is全停顿
For example , the application logic has to be paused once GC
it is needed . If the time for is too long, it may cause problems such as page freezes for users.60ms
60ms
GC
Parallel collection introduces multiple auxiliary threads to process at the same time, which can effectively shorten the time consumed by the garbage collection process, but garbage collection still occupies the time of the main thread, so during the garbage collection process, objects in memory are static (not executed The reference relationship of the js script object will not change )
The new generation objects generally adopt a parallel strategy. During the garbage collection process, multiple threads are started to be responsible for the garbage cleaning operation in the new generation.
2. Incremental marking and lazy cleaning
Although the parallel recycling strategy we mentioned above can increase the efficiency of garbage collection and can be well optimized for the new generation garbage collector, it is actually a full-stop garbage collection method. For the old generation, it The internal storage is some relatively large objects, GC
even may still consume a lot of time
Increment is to divide the process of GC
marking into many small steps. After each small step is executed, the application logic is executed for a while (intermittent execution), so that a round GC
of marking is completed after alternating multiple times, which can effectively avoid the main thread. Blockage, but there will be two problems, one is to execute the task program after each small GC
mark is executed, and then how to restore it , and secondly, what to do if the marked object reference relationship in the memory is modified when the task program is executed
three-color marking method
The three-color marking method uses two marking bits for each object and a marking worksheet to realize marking, and the two marking bits encode three colors: white, gray, and black
- White refers to unmarked objects (eventually collected)
- Gray means that it is marked, and the member variable (the reference object of the object) is not marked
- Black means that both itself and member variables are marked
After using the three-color marking method, when we resume execution, we can directly judge whether the entire marking is completed by whether there are gray nodes in the current memory. If there are no gray nodes, we will directly enter the cleaning stage. The node starts and continues to execute.
write barrier
If there is a situation where the reference relationship of the object is modified during the execution of js, there are generally two types, one is that the object that has been marked as black or gray is no longer referenced, and the other is that a new object is referenced. The former has little effect and will still be cleaned up in the next garbage collection process, but the latter is a newly referenced object, so the object will be marked as white, so that the object may be cleaned up in the next round of recycling .
In order to solve this problem, V8 uses 写屏障 (Write-barrier)
a mechanism , that is, once a black object references a white object, this mechanism will force the referenced white object to be changed to gray, so as to ensure that the next incremental GC
marking phase can be marked correctly.
lazy cleaning
Incremental marking is actually just marking active objects and inactive objects. For the real cleanup and release of memory, V8 uses lazy sweeping (Lazy Sweeping)
After incremental marking is complete, lazy cleanup begins. After the incremental marking is completed, if the current available memory is enough for us to quickly execute the code, in fact, we don't need to clean up the memory immediately, we can delay the cleaning process a little bit, let JavaScript
the script All inactive object memory can be cleaned up one by one as needed until all inactive object memory is cleaned up, followed by incremental marking.
Pros and Cons of Incremental Marking vs. Lazy Cleanup
Advantages: The pause time of the main thread is greatly reduced, and the process of user interaction with the browser becomes smoother
Disadvantages: Does not reduce the total pause time of the main thread, and even slightly increases it
3. Concurrent recycling (Concurrent)
During the execution JavaScript
of , the auxiliary thread can complete the garbage collection operation in the background. When the auxiliary thread is performing garbage collection, the main thread can also execute freely without being suspended. But it needs to consider that JavaScript
when , the object reference relationship in the heap may change at any time.
New generation garbage collection adopts parallel strategy to improve collection efficiency
The old generation garbage collection will use these three optimization strategies in a mixed manner. In the marking phase, it will be marked in a concurrent form (without affecting the main thread), and in the cleaning phase, it will be in parallel. In addition, in the cleaning phase, incremental batching Perform cleanup tasks interspersed between js tasks
Summarize
This article mainly summarizes the optimized garbage collection mechanism of the V8 engine. In addition to the recycling methods of the new and old generations, there are three optimization strategies. If there are mistakes, please correct me, thank you very much.