2022-04-29 16:29:45【Heapdump performance community】
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 .
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
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 .
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 .
Chrome 45（ Left ） And Chrome 43 On Gmail Memory usage ：https://youtu.be/ij-AFUfqFdI
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 .
author[Heapdump performance community],Please bring the original link to reprint, thank you.
The sidebar is recommended
- HTTP becomes HTTPS, self-made certificate
- Self inspection list of a [qualified] front-end Engineer
- Analyzing the principle of HTTPS encryption
- Difference and principle between websocket and http
- Use of elementui scroll bar component El scrollbar
- Nginx security optimization
- GAC group has become the first pilot enterprise of "yueyouhang". Blessed are the car owners in Guangdong!
guess what you like
Loki HTTP API usage
Front end experience
Filters usage of table in elementui
Grain College - image error when writing Vue with vscode
Utility gadget - get the IP address in the HTTP request
Could not fetch URL https://pypi.org/simple/pytest-html/: There was a problem confirming the ssl cer
- Function of host parameter in http
- Use nginx proxy node red under centos7 and realize password access
- Centos7 nginx reverse proxy TCP port
- In eclipse, an error is reported when referencing layuijs and CSS
- Front end online teacher Pink
- Learn to use PHP to insert elements at the specified position and key of the array
- Learn how to use HTML and CSS styles to overlay two pictures on another picture to achieve the effect of scanning QR code by wechat
- Learn how to use CSS to vertically center the content in Div
- Learn how to use CSS to circle numbers
- Learn to open and display PDF files in HTML web pages
- The PHP array random sorting function shuffle() randomly scrambles the order of array elements
- JQuery implements the keyboard enter search function
- Vue environment construction -- scaffold
- Build a demo with Vue scaffold
- Using vuex in Vue projects
- Use Vue router in Vue project
- 26 [react basics-5] react hook
- 07 Chrome browser captures hover element style
- Solution to the blank page of the project created by create react app after packaging
- Nginx learning notes
- Less learning notes tutorial
- Vue2 cannot get the value in props in the data of the child component, or it is always the default value (the value of the parent component comes from asynchrony)
- LeetCode 217. Determine whether there are duplicate elements in the array
- I'll write a website collection station myself. Do you think it's ok? [HTML + CSS + JS] Tan Zi
- Front end browser debugging tips
- Several ways of hiding elements in CSS
- node. Js-3 step out the use of a server and express package
- CSS matrix function
- Fastapi series - synchronous and asynchronous mutual conversion processing practice
- How to extend the functionality of Axios without interceptors
- Read pseudo classes and pseudo elements