当前位置:网站首页>Garbage first of JVM garbage collector
Garbage first of JVM garbage collector
2022-07-06 17:16:00 【TiggerRun】
Garbage collector
Note navigation
Garbage First GC
Record JVM Learning notes , Understanding may be biased , Hope to point out .
G1 Garbage collector
key word :Region、 Partition algorithm 、 The delay is controllable 、 Pause prediction model 、 parallel 、 Concurrent 、RSet、MixedGC
G1 What we hope to achieve : Improve throughput as much as possible with controllable delay
summary
- G1 It's a
parallel 、 ConcurrentDual garbage collector , Divide the memory into many irrelevant areas (RegionPhysically discontinuous ) - G1 Avoid the whole heap in a planned way GC,
Each time according to the allowed collection time , Give priority to collecting the most valuable Region - G1 Facing the server application , It is mainly equipped with
Multicore CPU And large memory machines - G1 stay JDK7 Officially enabled in ,JDK9 The default garbage collector after
Memory allocation policy
stay G1 Two memory allocation strategies are used in the garbage collector at the same time
- Pointer collision : In a Region Allocate memory for new objects through pointer collision
- Free list : Maintain a LinkedList To save free Region
Keep the idea of generational collection
- G1 Still keep the idea of generations , Or divide the heap memory into
The new generation、Old age - The memory distribution is no longer fixed ,G1 Divide memory into multiple
Region - Region Sure
dynamicPlay different roles (The new generation、Old age、Humongous), More than one size Region Objects of will be stored in continuous HumongousRegion in , Usually, it will be treated as the elderly generation .

Spatial integration
- G1 There is defragmentation ,G1 Memory reclamation of Region In units of ,Region Between
Copy algorithm, On the whole, it can be regarded as a label compression algorithm .
Pause prediction statistical model
G1 adopt The mean attenuation For each Region Time consumption of recycling 、 Calculate the amount of garbage and other measurable costs to obtain a series of statistical values . The mean value of attenuation can be expressed Current Average state , Use statistics to predict that garbage collection will start now , Which one Region There will be better benefits . Maintain a Priority list , So it's called Garbage First Recycling benefits first Region.
Solutions for cross region references

Card table
Card table (CardTable), By memory card (Card) constitute
every last Region The default in accordance with the 512kb It is divided into many cards to form a card table ,RSet It records the location of the card, not the object itself
Memory set
Memory set (Remember Set,R Set), Is to solve cross Region Object reference of , stay YoungGC when , If an object is referenced by an older object , Should not be cleared , But because of G1 The heap memory of the collector is partitioned , You need to traverse the whole heap to get the reference relationship of the object , stay YGC It is unreasonable to traverse the entire heap memory .
every last Region All assigned a RSet, Stored the reference relationship of the object (Region1 Of Region1Card_x Refer to the Region2 Of Region2Card_y), After having such a reference relationship , Reachability analysis only needs to obtain GC Roots, There is no need to traverse the entire heap space
Detailed explanation of recycling stage
YOUNG GC Stage
- Root scan ( Initialization tag )
- Update dirty card queue ( Maybe the reference relationship of the card was modified during the code execution ), to update Rset( Sync ), This ensures what you get Rset The reference relationship of is up-to-date
- Handle Rset, Identify those cited by older generations Eden District 、S Objects in the zone , That is, tell the garbage collector that this object is not garbage
copy object,Eden Objects that live in the area will be copied to S Free area of zone , Age goes up , Those who reach the threshold enter the elderly area , Some objects will be promoted to the elderly area( Because it's a replication algorithm ,G1 Realize defragmentation )- Handling references , Caused by copying objects Region change , So update Rset
Concurrent tagging phase
The process of marking garbage is time-consuming, because it needs to traverse the reference relationship , But this process is executed concurrently , Not at all STW,GC Threads and user threads work at the same time .
- Concurrent Tags : If you find that Region It's all garbage and will be carried out directly
Real time recyclingInstead of waiting for the next stage . - Mark again (
STW): Because the process of concurrent marking is concurrent , The user thread may have modified the reference relationship , Therefore, the marking needs to be corrected . - Exclusive cleanup (
STW): Calculate each Region Recovery ratio of , And sort it out , Prepare for the next phase - Concurrent cleanup : Identify and completely clean up idle Region
MIXED GC Stage
- In this stage , rate of recovery 100% Of Region It has been cleared in real time , Only some garbage is left Region
- When the heap memory usage reaches a certain threshold
-XX:G1MixedGCLiveThresholdPercentafter , Automatic mixing and recycling - distinguish MixedGC and FullGC,MixedGC Only part of the old age is recycled
- Old garbage memory segmentation will be divided 8 Time
-XX:G1MixedGCCountTargetTo recycle , Those with high recycling ratio will be recycled first - Of course, it is not necessarily implemented 8 Time , because
-XX:G1HeapWastePercentSpecifies the allowable waste space ratio
G1 The defects that exist
- Extra memory space
- Additional execution load
G1 The applicable scenarios of
- The first is the server application 、 Large memory 、 many CPU processor
- Application requirements GC It's low latency , The pause time is controllable
- Can be substituted for CMS
Be careful
The pause time is not as short as possible, If the pause is too short , It may result in only a few being processed at a time Region, Object generation is faster than garbage disposal , It will eventually lead to FullGC
Common parameters
| Parameter name | The default value is | effect |
|---|---|---|
| -XX:+UseG1GC | JDK9 Later versions use G1 | start-up G1 Garbage collector |
| -XX:G1HeapRegionSize | 1m | Set up Region Size (1m~32m Must be 2 The power of ) |
| -XX:MaxGCPauseMillis | 200ms | Set the maximum pause time |
| -XX:ParallelGCThreads | 8 | Number of parallel threads |
| -XX:ConcGCThreads | 2 | Number of concurrent threads , recommend 1/4 Parallel thread |
| -XX:InitiatingHeapOccupancyPercent | 45% | Set the memory usage threshold that triggers the concurrency flag |
| -XX:G1MixedGCCountTarget | 8 | MixedGC The stage is completed in several times |
| -XX:G1MixedGCLiveThresholdPercent | 65% | Set trigger MixedGC Heap memory usage threshold |
| -XX:G1HeapWastePercent | 10% | Set the allowable heap memory waste |
Optimization Suggestions
Young generation size
Use G1 Garbage collector , Don't passXmn、-XX:NewRatioSet a fixed younger generation size , It is to hand over to G1 Garbage collector dynamic settings , Otherwise, the target pause time will be overwritten .The pause time should not be set too small
边栏推荐
- Resume of a microservice architecture teacher with 10 years of work experience
- Flink 解析(五):State与State Backend
- Mongodb learning notes
- Akamai 反混淆篇
- Yao BanZhi and his team came together, and the competition experts gathered together. What fairy programming competition is this?
- DOS 功能调用
- IDEA断点调试技巧,多张动图包教包会。
- On the clever use of stream and map
- Install docker under windows10 (through Oracle VM VirtualBox)
- Logical operation instruction
猜你喜欢

Compile homework after class

Log4j2 major vulnerabilities and Solutions

字节跳动技术面试官现身说法:我最想pick什么样的候选人

JVM garbage collector part 2

这群程序员中的「广告狂人」,把抖音广告做成了AR游戏

Programmer orientation problem solving methodology

Idea resolving jar package conflicts

Thank you for your invitation. I'm in the work area. I just handed in the code. I'm an intern in the next ByteDance

Eight part essay that everyone likes

汇编语言段定义
随机推荐
服务器端渲染(SSR)和客户端渲染(CSR)的区别
À propos de l'utilisation intelligente du flux et de la carte
JVM运行时数据区之程序计数器
MySQL optimization notes
mysql的合计/统计函数
Activiti目录(一)重点介绍
Activiti directory (III) deployment process and initiation process
Fdog series (V): use QT to imitate QQ to realize login interface to main interface, function chapter.
The difference between URI and URL
Akamai浅谈风控原理与解决方案
CentOS7上Redis安装
算数运算指令
暑假刷题嗷嗷嗷嗷
Data transfer instruction
Flink 解析(四):恢复机制
MySQL日期函数
MySQL数字函数
MySQL date function
Alibaba cloud server docker installation mysql5.5
vscode