V8 know
- Is a mainstream JavaScript execution engine
- V8 uses just-in-time compilation
- V8 memory limit: 1.5 GB for 64-bit system and 800 MB for 32-bit system.
V8 garbage collection strategy
- Adopt the idea of generational recycling
- Memory is divided into new generation, old generation
- Different algorithms are used for different objects
The common GC algorithm in V8
- Generational recycling
- Space to copy
- Mark clear
- Tag to sort out
- Mark the incremental
How does V8 recycle new generation storage objects
Memory allocation
- V8 memory space is divided into two parts, the left side is the new generation storage area, the new generation storage area is divided into two storage areas, the right side is the old generation storage area
- Little space to store the new generation object (32 m | 16 m, corresponding to 64 – bit and 32-bit system)
- Cenozoic refers to objects that have a short life span
New generation object recovery implementation
- The recycling process adopts assignment algorithm + label sorting
- The new generation of memory is divided into two equal size Spaces
- The used space is From, and the free space is To
- Live objects are stored in the From space
- The live object is copied To the To space after the tag is collated
- The space exchanged between From and To is released
Recovery Details
- Promotions may occur during copying
- Promotion is moving the new generation to the old generation
- New generation objects that are still alive in a GC round need to be promoted
- To space usage above 25% requires promotion
How does V8 reclaim old generation store objects
- 64-bit OPERATING system 1.4 GB, 32-bit operating system 700 MB
- A legacy object is an object that has a long lifetime, such as a global variable or a variable generated by a closure
Old generation object recycling implementation
- It mainly adopts the algorithm of mark clearing, mark finishing and increment mark
- Firstly, the garbage space is recovered by token clearing, mainly by token clearing algorithm. Although space fragmentation will occur, this algorithm is faster and has better performance
- When the new generation objects move to the old generation storage area, that is, promotion, and the old generation storage area is insufficient memory space, the new generation will trigger the tag sorting algorithm
- Incremental marking is used for efficiency optimization
The problem
- Why don’t old generation stores use the same methods as new generation stores?
Because the old generation storage area is relatively large, if you split the space in two will waste hundreds of megabytes of space; 2. Why should the new generation storage area split the space in two, which is not a waste of space? Because the new generation storage area occupies a small space, the space waste caused by dividing into two Spaces is more beneficial than harmful compared with the recycling efficiency brought by them. It can be said that space is used to exchange time
How does incremental markup optimize garbage collection
The incremental marking algorithm is to split the marking process and run alternately with the program execution. When the marking stage is completed, the clearing operation is carried out to complete the recycling. This may seem like a lot of pauses, but the pauses are not visually significant, as the V8 engine takes at most one second to collect 1.5 gigabytes of garbage using the non-incremental tag algorithm.