Why do we need garbage collection (GC)?
What is Garbage Collection Garbage collection mechanism is also called Waste Generation When an object has no variables or properties referencing it, we will never be able to operate on the object. This object is garbage. Too many such objects will take up a lot of memory space and slow down the program. For example: Here I first declared a Person variable, which references the object {name: "江流", age: 20}. Then I pointed this Person variable to another object {name: "心猿", age: 5000}. The previously referenced object now becomes a useless object and can never be used to operate on it. This kind of object is garbage. If there are too many garbage objects, they will take up a lot of space. If they are not released, it will affect system performance and even cause the program to crash. Therefore, garbage collection is needed to release this part of memory. We do not need and cannot perform garbage collection in this process. All we need is to set the objects that are no longer used to null. Garbage Collection StrategyThe main concept of memory management in JavaScript is reachability. It roughly means that values that can be accessed or used in some way need to be kept in memory, and values that cannot be accessed or used need to be recycled by the garbage collection mechanism. The garbage collection process is not performed in real time because JavaScript is a single-threaded language. Each time garbage collection is executed, the program application logic will be paused. After the garbage collection is completed, the application logic will be executed again. This behavior is called a full pause, so garbage collection is generally performed when the CPU is idle. How to find the so-called garbage in some way is the focus of garbage collection, so the following common algorithm strategies, but here we only talk about the first two:
Reference count tagsStrategic thinking:
For example: let a = { name: "Jiang Liu", age: 20 }; //At this point the reference count of the object is marked as 1 (a reference) let b = a; //At this point the object's reference count is marked as 2 (a, b references) a = null; //At this point the object's reference count is marked as 1 ((b reference)) b = null; //At this point the object's reference count is marked as 0 (no variable reference) ... //Wait for GC to reclaim this object But this approach has a serious problem – circular references Problems caused by circular references In a function, the property of object A points to object B, and the property of object B points to object A. After the function is executed, the counters of objects A and B will not be 0, which affects the normal GC. For example, the following example: function test() { let A = new Object(); let B = new Object(); A.pointer= B; B.pointer = A; } test(); When the properties of object A and object B reference each other, according to the reference counting strategy, their reference counts are both 2. However, after test() is executed, after the function is executed, the data objects A and B in the function scope should be destroyed by GC. If executed multiple times, it will cause serious memory leaks. WorkaroundAt the end of the function, set it to null // Cut off the reference relationship A = null; B = null; Advantages and disadvantages of reference counting algorithmsadvantage:
shortcoming:
Mark-and-Sweep AlgorithmCore Idea It is completed in two phases: marking and clearing. Approximate process:
Advantages and disadvantages of mark-sweep algorithmadvantage:
shortcoming: After garbage collection, the memory locations of the remaining objects remain unchanged, which results in discontinuous free memory space. This will cause memory fragmentation, and since the remaining space is not a whole block, memory allocation becomes a problem. Mark-Sweep Algorithm The Mark-Compact algorithm can effectively solve this problem. After marking, the Mark-Compact algorithm will move the objects that do not need to be cleaned up to one end of the memory, and finally clean up the memory at the boundary. Garbage collection in V8 engine
Use different algorithms for different objects: (1) New generation: objects have a shorter survival time. New objects or objects that have only been garbage collected once. (2) Old generation: objects survive longer. An object that has undergone one or more garbage collections. Recycling new generation objectsThe new generation objects are mainly recycled by using the copy algorithm (Scavenge algorithm) plus the mark-sweep algorithm. The specific implementation of the Scavenge algorithm mainly uses the Cheney algorithm. Object promotion mechanismThe new generation that survives a round of GC needs to be promoted. Recycling old generation objectsThe mark-sweep, mark-compact, and incremental mark algorithms are mainly used to recycle old generation objects. The mark-sweep algorithm is mainly used, and the mark-compact algorithm is only used when memory allocation is insufficient.
Reference Documents:JS garbage collection mechanism JavaScript GC garbage collection mechanism SummarizeThis article ends here. I hope it can be helpful to you. I also hope you can pay more attention to more content on 123WORDPRESS.COM! You may also be interested in:
|
<<: Several methods of calling js in a are sorted out and recommended for use
>>: Front-end development general manual (including tools, websites, experience, etc.)
This article shares the specific code for JavaScr...
After installing docker, there will usually be a ...
IFNULL(expr1,expr2) If expr1 is not NULL, IFNULL(...
Preface I believe that everyone has had experienc...
The first thing to do is to pick a good browser. ...
Rendering pipeline with external css files In the...
Route Jump this.$router.push('/course'); ...
There are three types of virtual hosts supported ...
Ubuntu is a relatively popular Linux desktop syst...
Table of contents Preface What is index pushdown?...
Table of contents Storage Engine Memory Managemen...
First of all, what is a font icon? On the surface...
Introduction to vi/vim They are both multi-mode e...
Table of contents 1 Question 2 Methods 3 Experime...
Table of contents Previous words Synchronous and ...