当前位置:网站首页>Yyds dry inventory brief chrome V8 engine garbage collection

Yyds dry inventory brief chrome V8 engine garbage collection

2022-06-27 16:12:00 51CTO

V8 It is a more mainstream at present JavaScript Execution engine , Using just in time compilation , It's very fast .V8 Your memory is limited , such as 64 The upper limit of bit operating system is 1.4T, The lower limit is 700M,32 The upper and lower limits of the bit operating system are 64M and 32M.

V8 Adopt the garbage collection strategy of generational recycling , Memory is divided into new generation and old generation , Different corresponding algorithms are used for different objects .

#yyds Dry inventory # sketch chromeV8 Engine garbage collection _v8

#yyds Dry inventory # sketch chromeV8 Engine garbage collection _v8_02

Above, V8 Memory allocation diagram of , You can see V8 Memory space is divided into two parts . Sinister from and to It's the new generation , It takes up less space (32M|16M), The Cenozoic here refers to the storage area with short survival time . The red part on the right is the older generation storage area with longer survival time .

​V8 frequently-used GC The algorithm has the following 5 Kind of :

  • Generational recycling
  • Space replication
  • Mark clear
  • Tag to sort out
  • Mark increment

Among them, the new generation adopts replication algorithm and label sorting for garbage collection , Older generations use markers to clear 、 Mark up and incremental mark up for garbage collection .


V8 New generation object recycling implementation

#yyds Dry inventory # sketch chromeV8 Engine garbage collection _ Bit operation _03

The picture above shows V8 Implementation diagram of new generation object recycling , The combination of replication algorithm and tag sorting is used for garbage collection . Two equal spaces of the Cenozoic memory area ,From Represents the space used to store active objects ,To Represents free space .V8 The new generation of object recycling is to copy the objects to... After finishing the sorting through marking To, And then To and From Space exchange , And free up the space occupied by the sorted useless objects . It should be noted that , Copying grooming objects to To There may be a promotion . Promotion refers to moving new generation objects to older generation storage . There are usually two conditions for promotion , One is a round of GC The new generation of objects who are still alive can be promoted , The second is To The utilization rate of space exceeds 25%.

V8 Old generation object recycling implementation

V8 The recycling process of the old generation adopts mark removal 、 The combination of mark sorting and mark increment . Generally, when garbage collection is carried out, the garbage space will be recycled through mark removal , But when the new generation moves to the old generation , And when the older generation doesn't have enough memory , Then the space will be optimized through tag sorting , And use incremental markers to optimize efficiency .

#yyds Dry inventory # sketch chromeV8 Engine garbage collection _ Garbage collection _04

​ Mark increment is actually a method of marking the mark operation , Make the schedule reasonable . This sentence may be a little windy , Simply put, in garbage collection , Let the marking system distinguish different time periods when marking , Mark and execute respectively , Let the two operations be separated , To optimize the timing , This will make the page feel smoother .

原网站

版权声明
本文为[51CTO]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/178/202206271523396007.html