A directory
What’s the difference between a free front end and a salted fish
directory |
---|
A directory |
The preface |
Triple stack garbage collection |
Four piles of garbage to recycle |
Cenozoic and old generation |
5.1 New Generation – Secondary garbage collector |
5.2 Old Generation – Main garbage collector |
Six full stop |
Vii References |
The preface
Raw data types are stored in stack space, and reference data is stored in heap space.
However, after some data is used, it may no longer be needed. We call this data junk data.
If this garbage data is kept in memory, memory will be used more and more.
So we need to recycle these garbage data to free up the limited memory space.
This is where garbage collection comes in.
Garbage collection strategies are divided into manual collection and automatic collection:
- Manual recovery: C/C++ can pass
free()
This code determines when memory is allocated and when memory is destroyed. - Automatic collection: Garbage data generated by JavaScript, Python, Java, and so on is released by the garbage collector.
Triple stack garbage collection
When the function completes execution, the JavaScript engine destroys the execution context (variable environment, lexical environment, this, outer) stored in the stack by moving the ESP pointer down (the pointer that registers the current execution state of the call stack).
If there is code:
function foo(){
var a = 1;
var b = { name: 'jsliang' };
function showName(){
var c = 2;
var d = { name: 'zhazhaliang' };
}
showName();
}
foo();
Copy the code
In the code above, according to the browser policy:
- Start with the global execution context
- And then to
foo
Function execution context - The last to
showName
Function execution context
According to the characteristics of the stack, the JavaScript situation should be:
The call stack |
---|
showName Function execution context |
foo Function execution context |
Global execution context |
Then the release process begins:
- Perform to
showName
Function when createdshowName
Function execution context, and willshowName
The execution context of a function is pushed onto the stack. - At the same time, we have a pointer to the current execution state (called
ESP
) points to the call stackshowName
The execution context of a function, indicating that it is currently executingshowName
Function. - when
showName
After the function is executed, the function execution process entersfoo
Function, at which point ESP moves down tofoo
The execution context of a function. The move down operation is destructionshowName
Function executes the procedure of the context.
If the showName function is followed by another call, it overwrites the contents of the showName function and holds the execution context of the other function.
That is, when a function finishes executing, the JavaScript engine destroys the execution context in the save stack by moving ESP down.
Four piles of garbage to recycle
Above, we collected garbage from the stack via ESP:
function foo(){
var a = 1;
var b = { name: 'jsliang' };
function showName(){
var c = 2;
var d = { name: 'zhazhaliang' };
}
showName();
}
foo();
Copy the code
But the b and D inside are still in the heap.
To collect garbage data from objects, you need a garbage collector in JavaScript.
Cenozoic and old generation
Characteristics of intergenerational hypothesis:
- Most objects are short-lived
- Objects that are not destroyed live longer
These two characteristics apply not only to JavaScript, but also to most dynamic languages, such as Java, Python, and so on.
In V8, the heap will be divided into two regions: new generation and old generation. The new generation stores objects with a short life time, and the old generation stores objects with a long life time.
The new area is not as large as the old area, so V8 uses two different garbage collectors to efficiently implement garbage collection:
- Secondary garbage collector. Mainly responsible for the new generation of garbage recycling.
- Main garbage collector. Mainly responsible for old generation garbage recycling.
5.1 New Generation – Secondary garbage collector
Algorithm: Scavenge
Object area | The free zone |
---|---|
. | . |
Principle:
- The Cenozoic space is divided into two regions, one is the object region and the other is the idle region.
- New objects are stored in the object area. When the object area is nearly full, a garbage cleanup operation is performed.
- The garbage in the object area is marked first, and when marked, the surviving objects are copied to the free area
- 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.
Object promotion strategy: Objects that survive two garbage collections are moved to the old area.
5.2 Old Generation – Main garbage collector
- Algorithm: Mark-sweep algorithm
- Principle:
- Tagging: The tagging phase is to start with a set of root elements and 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 cannot be reached can be judged as junk data.
- Clear: The garbage data is cleared.
Multiple execution of the mark-clear algorithm on a piece of memory results in a large number of discrete memory fragments. Too much fragmentation causes large objects to fail to allocate enough contiguous memory.
This is where you need to mark up.
- Algorithm: Mark-compact algorithm
- Principle:
- Tagging: As with mark-clean tagging, you start with a set of root elements, recursively traverse the set of root elements, and mark the reachable elements as active objects during this traversal.
- Defragment: Move all surviving objects to one end of memory
- Clear: Clears up memory outside the end boundary
Six full stop
Since JavaScript is single-threaded, once the garbage collection algorithm is executed, the executing JavaScript script needs to be paused and resumed after the garbage collection is complete.
This behavior is called stop-the-world.
If there is a lot of data in the heap, it will take time to recycle and the page will lag. Therefore, to reduce this lag, V8 divides the tagging process into sub-tagging process, and the garbage collection markup and JavaScript application logic are alternated.
- Optimization algorithm: Incremental Marking algorithm
- Principle:
- In order to reduce the old generation of garbage collection caused by the backlog
- V8 breaks a complete garbage collection task into many smaller tasks
- Alternate garbage collection tags with JavaScript application logic
Vii References
- JavaScript Advanced – Memory mechanism (emoticons)
Jsliang’s document library is licensed by Junrong Liang under the Creative Commons Attribution – Non-commercial – Share alike 4.0 International License. Based on the github.com/LiangJunron… On the creation of works. Outside of this license agreement authorized access can be from creativecommons.org/licenses/by… Obtained.