current position:Home>Learn more about the garbage collection engine of chrome V8 JavaScript engine

Learn more about the garbage collection engine of chrome V8 JavaScript engine

2022-04-29 16:29:45Heapdump performance community

JavaScript Performance is still Chrome One of the key aspects of values , Especially in achieving a smooth experience . from Chrome 41 Start ,V8 Use a new technology to improve Web Application responsiveness , The way is to hide expensive memory management operations in small 、 Otherwise, unused idle time blocks . therefore ,Web Developers should expect smoother scrolling and butter animation ( Refers to an animation that is as smooth as butter ), And because of garbage collection, it greatly reduces the number of catons .


Many modern language engines , for example Chrome Of V8 JavaScript engine , Dynamically manage memory for running applications , So developers don't have to worry about . The engine periodically passes the memory allocated to the application , Determine what data is no longer needed , And clear it to free up space . This process is called garbage collection .


stay Chrome in , We strive to provide smooth communication 60 frame (FPS) The visual experience of . Even though V8 An attempt has been made to perform garbage collection in a small block , But larger garbage collection operations can and do occur at unpredictable times —— Sometimes in the middle of the animation —— Pause execution and block Chrome achieve 60 FPS The goal of .


Chrome 41 Includes one for Blink Task scheduler of rendering engine , It can prioritize delay sensitive tasks , In order to ensure that Chrome Stay responsive and agile . In addition to being able to prioritize work , The task scheduler also focuses on how busy the system is 、 The tasks to be performed and the urgency of these tasks . therefore , It can estimate Chrome When may be idle , And how long it is expected to remain idle .


When Chrome When displaying animation on a web page , This will happen . The animation will be in 60 FPS Update the screen at the same speed , to Chrome about 16.6 Milliseconds to execute the update . therefore ,Chrome Processing of the current frame will begin immediately after the previous frame is displayed , Perform input... For this new frame 、 Animation and frame rendering tasks . If Chrome In less than 16.6 All this work was done in milliseconds , So before you need to start rendering the next frame , It has nothing else to do . Chrome The scheduler makes V8 In the Chrome When idle, use this idle time period by scheduling special idle tasks .

 Frame rendering with idle tasks - Deepen understanding Chrome V8 javascript engine Garbage collection engine - HeapDump Performance community
chart 1: Frame rendering with idle tasks

Idle tasks are special low priority tasks , When the scheduler determines that it is idle . There is an idle due date , This is the scheduler's estimate of what it expects to remain idle . In the figure 1 In the animation example of , This will be the time to start drawing the next frame . In other cases ( for example , When there is no activity on the screen ), This may be when you plan to run the next pending task , Cap of 50 millisecond , In order to ensure that Chrome Remain responsive to unexpected user input . It can be used to estimate how many tasks are delayed by entering an idle or stuck response date .


Garbage collection done in idle tasks is critical to 、 Delay sensitive operations are covert . This means that these garbage collection tasks are “ free ” Accomplished . In order to understand V8 How does this work , It is necessary to review V8 Current garbage collection strategies .


Deepen understanding V8 Garbage collection engine

V8 Use generational garbage collectors , take Javascript The heap is split into small younger generations for new allocation objects and large older generations for long-term survival objects . Because most objects die when they are young , This generational strategy enables garbage collectors to be used in younger generations ( It is called clearing ) Perform regular... In 、 Short time garbage collection , Without having to track objects in older generations .


notes : What is generational garbage collection ?

Generational garbage collection is tracking garbage collection using intergenerational assumptions . Objects are passed down from generation to generation . New objects are assigned in the Cenozoic , If they survive , Then promoted to the old age . Objects in the older generation are condemned less frequently to save CPU Time .

An object usually rarely refers to younger objects . therefore , Objects in the next generation are also often rarely referenced by objects in the younger generation . This means that in the process of collecting the younger generation, the scanning of the old age can be done more effectively through the memory set .
In some purely functional languages ( That is, there is no update ) in , All references are backwards in time , under these circumstances , Memory sets are unnecessary .


The younger generation uses half space (semi-space) Allocation policy , New objects are initially allocated in the active half space of the younger generation . Once the half space becomes full , Cleanup moves the active object to another half space . Objects that have been moved once are promoted to the old age , Considered a long life (long-living) Of . Once the active object is moved , The new half space will become active , And any remaining dead objects in the old half space will be discarded .


therefore , The duration of the younger generation cleanup depends on the size of the active object in the younger generation . When most objects become inaccessible in the younger generation , Clearing will soon (<1 ms). however , If most objects survive the cleanup process , The duration of clearance may be significantly prolonged .


When the size of active objects in the elderly generation exceeds the heuristic derivation (heuristically-derived) Limit time , The main collection of the entire heap will be performed . Older generations use mark and clear collectors with multiple optimizations (mark-and-sweep) To improve latency and memory consumption . The marking delay depends on the number of active objects that must be marked , For large Web Applications , Marking the entire heap may take more than 100 millisecond . To avoid long pauses in the main thread ,V8 It has long been possible to mark active objects in many small step increments , The purpose is to keep the duration of each marking step at 5 Under milliseconds .


After the tag , By cleaning up the entire older generation of memory , Free memory is again available to applications . This task is performed simultaneously by a dedicated cleaning thread . Last , Perform memory compression to reduce old age memory fragmentation . This task can be very time consuming , And only execute if there is a memory fragmentation problem .


Sum up , There are four main garbage collection tasks :


  • The elimination of the younger generation , Usually very quickly
  • Marking steps performed by incremental marking , It can be any length , Depending on the step size
  • Complete garbage collection , It could take a long time
  • Complete garbage collection with active memory compression , It could take a long time , But it will clean up the fragmented memory


In order to perform these operations during idle time ,V8 Publish garbage collection idle tasks to the scheduler . When these idle tasks are running , They are provided with deadlines they should meet . V8 The garbage collection idle time handler evaluates which garbage collection tasks should be performed to reduce memory consumption , Also observe deadlines to avoid future frame rendering or input delays .


If the allocation rate measured by the application shows that the younger generation may have expired before the next expected idle period , The garbage collector will perform younger generation cleanup during idle tasks . Besides , It calculates the average time spent on the most recent cleanup task , To predict the duration of future cleanup and ensure that it does not violate the deadline of idle tasks .


When the size of surviving objects in the old age is close to the heap limit , Start incremental marking . The incremental marking step can be scaled linearly according to the number of bytes to be marked . According to the measured average marking speed , The garbage collection idle time handler tries to put as much tagging work as possible into a given idle task .


If the old generation is almost full , And if the deadline provided to the task is estimated to be long enough to complete the collection , Schedule complete garbage collection during idle tasks . The collection pause time is predicted by multiplying the marking speed by the number of allocated objects . Only when the web page is idle for a long time , Will perform a full garbage collection with additional compression .


Performance evaluation


To assess the impact of running garbage collection in idle time , We use Chrome Telemetry performance benchmarking framework to evaluate the fluency of popular websites scrolling when loading . We are right. Linux Front on workstation 25 Sites and Android Nexus 6 Benchmark a typical mobile site on a smartphone , Both sites can open popular web pages ( Include Gmail、Google Docs and YouTube And other complex web applications ) And scroll its contents in a few seconds . Chrome Designed to keep 60 FPS Rolling speed , To get a smooth user experience .


chart 2 Shows the percentage of garbage collection scheduled in idle time . And Nexus 6 comparison , Faster hardware at the workstation results in more overall idle time , Thus, a larger proportion of garbage collection can be arranged in this free time (43% comparison Nexus 6 Upper 31%), So it's about 7% our jank indicators .

 Percentage of garbage collection that occurs during idle time - Deepen understanding Chrome V8 javascript engine Garbage collection engine - HeapDump Performance community
chart 2: Percentage of garbage collection that occurs during idle time

In addition to improving the smoothness of page rendering , These idle periods also provide an opportunity to perform more aggressive garbage collection when the page is completely idle . Chrome 45 The latest improvements in take advantage of this to significantly reduce the amount of memory consumed by idle foreground tabs . In the following video content, we introduce and Chrome 43 Compared to the same page in ,Gmail Of JavaScript How to reduce the memory usage of the heap when it is idle by about 45%.

Chrome 45( Left ) And Chrome 43 On Gmail Memory usage :

These improvements show that , You can hide garbage collection pauses by knowing more intelligently when to perform expensive garbage collection operations . Web Developers no longer need to worry about pausing garbage collection , Even for silky 60 FPS So is animation . As we push the boundaries of garbage collection scheduling , Please continue to pay attention to more improvements .

copyright notice
author[Heapdump performance community],Please bring the original link to reprint, thank you.

Random recommended