Garbage Collection strategy
1. Mark removal
- First, the corresponding recyclable objects are marked according to the reachability algorithm
- Recycle recyclable objects
Disadvantages: memory fragmentation, waste of spaceCopy the code
2. Mark finishing method
- First, the corresponding recyclable objects are marked according to the reachability algorithm
- Recycle recyclable objects
- Move all live objects to one side, next to each other (fixed memory fragmentation)
Disadvantages: It is inefficient to move live objects frequently with each garbage sweepCopy the code
3. Clear the reference count
Disadvantages: Circular reference, cannot be clearedCopy the code
V8 optimization for GC: generational garbage collection
- New generation garbage Collection: Replication algorithm, parallel collection
- Old generation garbage collection: tag collation, incremental tag, lazy cleaning, parallel collection, concurrent collection
Generational garbage collection
The heap memory is divided into new generation and old generation, and different garbage collectors are adopted to manage garbage collectionCopy the code
1. Recycling of waste of the new generation
- Generally, only 1 to 8 MB capacity is supported
- Garbage collection is conducted using the Scavenge algorithm, primarily using a replicative method (the Cheney algorithm) : the Cheney algorithm divides heap memory into used and free areas
- New objects are stored in the usage area, and when the usage area is nearly full, a garbage cleanup operation is performed
- Mark the active objects in the use area. After marking, the active objects in the use area are copied into the free area and sorted
- Clean up space occupied by inactive objects
- Finally, the original use area into the idle area, the original idle area into the use area
- When an object survives multiple copies, it is moved to the old generation to be managed
- If an object is copied to a free area and the free area occupies more than 25% of its space, the object is promoted directly to the old generation
- Parallel recovery
2. Old generation garbage recycling
- Label finishing
- Incremental tag
- Inert to clean up
- Parallel recovery
- Concurrent collector
Parallel recovery
Multithreading garbage collection to reduce the running time of the main thread
Incremental markup with lazy cleanup
1. Incremental mark
Break the process of a GC tag into small steps and let the application run for a while after each stepCopy the code
-
Three-color marking method: solve pause and resume problems
/ / | not behind the white is whiteWhite refers to objects that are not marked| no/behind/gray is whiteGray means that the object itself is marked and the member variable (a reference to the object) is not marked/ behind/black is black | | not grayBlack indicates that both self and member variables are markedCopy the code
-
Write barrier: Resolves the problem of modifying references in deltas
// Black refers to a gray object, but changed to refer to a white objectOnce a black object references a white object, this mechanism forces the referenced white object to be gray to ensure that the next incremental GC marking phase will be marked correctly. This mechanism is also known as strong trichromatic invarianceCopy the code
2. Lazy cleanup
If the current available memory allows us to execute the code quickly, we can delay the cleanup process a little bit, cleaning up as needed until all of the inanimate object memory is cleared, and then performing the delta markerCopy the code
Concurrent collector
During the execution of JavaScript, the helper thread can perform garbage collection in the background, so that the main thread does not have to runCopy the code
A memory leak
Not all unused object memory can be reclaimed, so when unused memory is not reclaimed in time, we call it a memory leakCopy the code
Common memory leak cases
- Global variables – Act as memory leaks: Name is defined globally in the example
function fn() {
// this. Name = "you"
name = "You and I."
}
console.log(name)
Copy the code
- Undestroyed timer and callback functions – shine as a memory leak
- Closures – Shine as a memory leak
- DOM references – as a memory leak
var elements = {
txt: document.getElementById("test")}function fn() {
elements.txt.innerHTML = "1111"
}
function removeTxt() {
document.body.removeChild(document.getElementById('test'))
}
fn() // The element with id test is referenced after execution
removeTxt() // It cannot be cleared completely
Copy the code
source
- Isboyjc: “Hardcore JS” do you really understand garbage collection