Hello everyone, the topic shared today is JavaScript memory management mechanism. This sharing will be described in the following three parts:
- js memory management and js garbage
- Common GC Algorithms
- Garbage collection for the V8 engine
js memory management and js garbage
About JavaScript memory management mechanism, I believe everyone knows something. Let's take a brief look at js memory management and js garbage. JavaScript memory management is automatically operated by JS and does not require human involvement. These memory managements include the following three items:
- Allocate memory space
- Use content space
- Free up content space
The js garbage refers to the time when the object is not referenced and the object cannot be accessed from the root, which can be called js garbage. The other parts, including references and reachable objects, are definitely familiar to everyone, so we won't talk about them any more. Now let's talk about the GC algorithm.
GC algorithm
The GC algorithm is actually to find garbage in memory, and release and reclaim space. The garbage mentioned here refers to the objects that the algorithm believes that the program no longer needs to be used, and the objects that cannot be accessed in the program.
Speaking of the GC algorithm, this is a relatively conceptual content, so let's briefly summarize it. GC is a garbage collector mechanism that finds garbage in memory to release space and reclaim space. Algorithms are the rules for finding and recycling while working. Common GC algorithms include reference counting, mark clearing, mark sorting, and generational recycling.
reference counting
Reference counting used to be mainly used in browsers below IE8, and now browsers are no longer used, so only a brief introduction will be given. The basic principle of reference counting is to record and track the number of times each value is referenced. When it is referenced, the count is increased by one, and when it is released, it is decreased by one. When the value is zero, it means that the memory where the value is located is no longer used, so the space occupied is released. . The advantage of reference counting is that the number of references is monitored in real time, so garbage collection can be collected in time, thereby minimizing program pause time. But it is also because it is running all the time, so the resource consumption and time overhead are large, and the circularly referenced objects cannot be recovered.
mark clear
Mark clearing is divided into two stages: marking and clearing. The core idea is to traverse all objects, find marked active objects, that is, the reachable objects mentioned above, clear unmarked objects, and reclaim the space of unmarked objects.
The above figure is a simple flow chart of global search. Among them, A, B, C, D, and E on the left represent objects that can be found, and a1 and b1 on the right represent circular reference objects. Among them, a1 is the reference count, and because the reference count is always in operation, the disadvantage of the circularly referenced object cannot be recovered, and the circularly referenced object can be found in reverse.
This is also the advantage of mark-sweep, which can solve the problem of reference recycling of object cycles. However, the disadvantage of mark-sweeping is that the space is fragmented and garbage objects cannot be recycled in time. Because it needs to be marked and then cleared, it cannot monitor the value in real time like a reference count, so it cannot maximize the use of space. The following figure provides a brief look at the space fragmentation characteristics of markup removal.
markup
As mentioned above, mark clearing has the disadvantage of space fragmentation, and mark sorting optimizes this disadvantage. As the name also suggests, markup cleanup is an enhancement of markup removal. The operation of mark sorting in the mark phase is the same as that of mark clearing, but in the clearing phase, sorting is performed first, and then clearing is performed. This method can effectively reduce fragmented space. Like mark-sweep, mark-sweep does not collect garbage objects in real time.
We have a simple and intuitive understanding of tag sorting through the following three pictures.
It can be seen that before garbage collection, the active space and the inactive space are mixed. After it is determined to be recycled, the marking and sorting will classify and sort the space, and organize the active space and the inactive space together, forming the result of the following figure:
After marking and clearing, you can avoid the recycling operation, avoid a large amount of fragmented space, and maximize the application of space.
After reading the GC algorithm, take the V8 engine as an example, let's take a look at the use of the GC algorithm in JS garbage collection.
Garbage collection for the V8 engine
V8 is a relatively mainstream JavaScript execution engine. It uses real-time compilation and has a fast processing speed. The memory of V8 is limited. For example, the upper limit of the 64-bit operating system is 1.4T, the lower limit is 700M, and the upper and lower limits of the 32-bit operating system are 64M and 32M respectively.
V8 adopts the garbage collection strategy of generational collection, divides the memory into two types: the new generation and the old generation, and adopts different corresponding algorithms for different objects.
The figure above is a schematic diagram of the memory allocation of V8. It can be seen that the V8 memory space is divided into two parts. The from and to on the left are the new generation, occupying a relatively small space (32M|16M). The new generation here refers to the storage area with short survival time. The red part on the right is the old generation storage area with a long survival time.
There are five commonly used GC algorithms in V8:
- Generational recycling
- space copy
- mark clear
- markup
- mark increment
Among them, the new generation uses the replication algorithm and mark sorting for garbage collection, and the old generation uses mark clearing, mark sorting and incremental mark for garbage collection.
V8 New Generation Object Recycling Implementation
The above picture shows the implementation diagram of V8 Cenozoic object recycling, which uses a combination of replication algorithm and tag sorting for garbage collection. Two equal-sized spaces in the new generation memory area, From represents the used space for storing active objects, and To represents the free space. The new generation object recycling of V8 is to copy the objects to To after finishing the sorting by marking and sorting, and then exchange the space between To and From, and release the space occupied by the sorted useless objects. Note that promotions may occur when copying collating objects to To. Promotion refers to moving young generation objects to old generation storage. There are usually two conditions for promotion. One is that the new generation objects that are still alive after a round of GC can be promoted, and the other is that the usage rate of the To space exceeds 25%.
V8 old generation object recycling implementation
The recycling process of V8 old generation adopts a combination of mark clearing, mark sorting and mark increment. Generally, during garbage collection, the garbage space is recovered by marking and clearing. However, when the new generation moves to the old generation and the memory of the old generation is insufficient, space optimization is performed by marking and sorting, and efficiency optimization is performed using incremental marking.
Tagging increments are actually a way of tagging tagging operations to make timing reasonable. This sentence may be a bit confusing. Simply put, during garbage collection, let the marking system divide different time periods when marking, mark and execute them separately, so that the operations of the two are spaced apart, so as to optimize the time arrangement, which will make the page Feels smoother.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。