当前位置:网站首页>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 、 Concurrent
Dual garbage collector , Divide the memory into many irrelevant areas (Region
Physically 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
dynamic
Play 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 recycling
Instead 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:G1MixedGCLiveThresholdPercent
after , 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:G1MixedGCCountTarget
To recycle , Those with high recycling ratio will be recycled first - Of course, it is not necessarily implemented 8 Time , because
-XX:G1HeapWastePercent
Specifies 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:NewRatio
Set 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
边栏推荐
猜你喜欢
逻辑运算指令
JVM garbage collector part 1
MySQL日期函数
TCP's three handshakes and four waves
Interpretation of Flink source code (III): Interpretation of executiongraph source code
Flink 解析(二):反压机制解析
Wu Jun trilogy insight (IV) everyone's wisdom
搭建flutter环境入坑集合
Description of project structure configuration of idea
Log4j2 major vulnerabilities and Solutions
随机推荐
Fdog series (III): use Tencent cloud SMS interface to send SMS, write database, deploy to server, web finale.
List集合数据移除(List.subList.clear)
Many papers on ByteDance have been selected into CVPR 2021, and the selected dry goods are here
Basic knowledge of assembly language
案例:检查空字段【注解+反射+自定义异常】
8086 segmentation technology
Coursera cannot play video
MySQL字符串函数
Flink 解析(四):恢复机制
Akamai浅谈风控原理与解决方案
mysql的列的数据类型详解
Logical operation instruction
Idea breakpoint debugging skills, multiple dynamic diagram package teaching package meeting.
汇编语言基础知识
Some feelings of brushing leetcode 300+ questions
逻辑运算指令
Activiti directory (V) reject, restart and cancel process
Fdog series (4): use the QT framework to imitate QQ to realize the login interface, interface chapter.
Idea resolving jar package conflicts
字节跳动春招攻略:学长学姐笔经面经,还有出题人「锦囊」