JavaScript has an automatic garbage collection mechanism. When a value loses its reference in memory, the garbage collection mechanism finds it according to a special algorithm, recycles it, and releases the memory.
1. The concept of garbage collection
When some data is not needed, these data are garbage data, and garbage occupies memory, so it needs to be recycled and cleaned up.
such as
let dog = new Object(); let dog.a = new Array(1);
- When JavaScript executes this code, it will first add an
dog
attribute to the global scope , and create an empty object in the heap, pointing to the address of the objectdog
. - Then an array of size 1 is created and the attribute address is pointed to
dog.a
. The memory layout diagram at this time is as follows:
If at this time, I assign another object to the a
property, the code is as follows:
dog.a = new Object()
The memory layout diagram at this time:
a
The direction of is changed, and the array objects in the heap become unused data. The technical term is unreachable data. This is the garbage data that needs to be collected.
Two, garbage collection algorithm
The first step: the reachable value in the mark space:
- Starting from the root node (Root), all objects are traversed.
- The objects that can be traversed are reachable.
- Objects that have not been traversed, unreachable
Step 2: Reclaim the memory occupied by the "unreachable" value
- After all markings are completed, all unreachable objects in the memory are cleaned up uniformly.
The third step, memory consolidation
- After frequently reclaiming objects, there will be a large amount of discontinuous space in the memory. The technical term is memory fragmentation.
- When a large number of memory fragments appear in the memory, if a larger contiguous memory needs to be allocated, there may be insufficient memory.
- So the last step is to defragment the memory
Three, generational collection
Browsers divide data into two types, one is temporary objects, and the other is permanent objects. These two types of objects correspond to different recycling strategies. Therefore, V8 divides the heap into two areas: the young generation and the old generation . The young generation stores temporary objects and the old generation stores long-term objects. And let the secondary garbage collector be responsible for the garbage collection of the new generation, and the main garbage collector will be responsible for the garbage collection of the old generation.
Temporary object:
- Most objects live in memory for a short time.
- For example, variables declared inside functions, or variables in block-level scope. When the execution of the function or code block ends, the variables defined in the scope will be destroyed.
- Such objects quickly become inaccessible and should be recycled quickly.
Secondary garbage collector
- Responsible for the garbage collection of the new generation, usually only supports 1~8 M capacity.
- Once it is detected that the space is full, garbage collection is performed.
- Because the secondary garbage collector operates more frequently, in order to perform efficiently, the space of the newborn area is generally set to be relatively small.
The Cenozoic is divided into two areas: one half is the object area and the other half is the free area .
The newly added objects are all put into the object area, and when the object area is almost full, a garbage cleanup will be performed.
- Mark all garbage in the target area first.
- After the marking is completed, the surviving objects are copied to the free area, and they are arranged in an orderly manner.
- The secondary garbage collector does not defragment. Because the free area is in order at this time, there is no fragmentation, and there is no need to organize it.
- After the copy is completed, the target area will be swapped with the free area. Put the objects that live in the free area into the object area.
- Complete garbage collection
Long-term object:
- Objects with a long life cycle, such as global ones,
window、DOM、Web API
etc. - Such objects can be recycled slowly.
Main garbage collector
Responsible for the garbage collection of the old generation, there are two characteristics:
- The object takes up a lot of space.
- The subject survives for a long time.
Use the mark-clear algorithm to perform garbage collection:
mark
- Starting from a set of root elements, recursively traverse the set of root elements.
- In this traversal process, the elements that can be reached are called active objects, and the elements that are not reached can be judged as garbage data.
Clear
- Directly clean up the data marked as junk.
Memory organization
- After multiple marking-clearing, a large number of discontinuous memory fragments will be generated, which requires memory arranging.
Fourth, incremental collection
If there are many objects in the script, the engine traverses the entire object at once, causing a long pause
- So the engine divides the garbage collection work into smaller chunks
- Process one part at a time, and process multiple times.
Five, leisure time collection
The garbage collector will only try to run when the CPU is idle to reduce the possible impact on code execution
Details: https://www.cnblogs.com/gg-qq/p/13862251.html
https://www.wolai.com/mary/bP7aj7Hm75FrZUsmLV7AY4#389vLfMeoPqDCLU2WLGgxc