当前位置:网站首页>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
边栏推荐
- Design of DS18B20 digital thermometer system
- DOS 功能调用
- Some feelings of brushing leetcode 300+ questions
- 唯有学C不负众望 TOP4 S1E6:数据类型
- Idea resolving jar package conflicts
- After the subscript is used to assign a value to the string type, the cout output variable is empty.
- 这群程序员中的「广告狂人」,把抖音广告做成了AR游戏
- Activit零零碎碎要人命的坑
- Brush questions during summer vacation, ouch ouch
- Flink 解析(五):State与State Backend
猜你喜欢
Activiti directory (V) reject, restart and cancel process
Shawshank's sense of redemption
Train 100 pictures for 1 hour, and the style of the photos changes at will. There is a demo at the end of the article | siggraph 2021
TCP的三次握手和四次挥手
The daemon thread starts redis and modifies the configuration file
JVM 垃圾回收器之Garbage First
Logical operation instruction
Activit fragmented deadly pit
List集合数据移除(List.subList.clear)
搭建flutter环境入坑集合
随机推荐
Flink 解析(五):State与State Backend
MySQL string function
JVM garbage collector part 1
唯有学C不负众望 TOP2 p1变量
IDEA断点调试技巧,多张动图包教包会。
Compile homework after class
Basic knowledge of assembly language
8086 segmentation technology
8086 CPU internal structure
js垃圾回收机制和内存泄漏
Activiti directory (I) highlights
吴军三部曲见识(五) 拒绝伪工作者
关于Stream和Map的巧用
TCP的三次握手和四次挥手
Conception du système de thermomètre numérique DS18B20
mysql的合计/统计函数
DOS 功能调用
Go language uses the thrift protocol to realize the client and service end reports not enough arguments in call to oprot Writemessagebegin error resolution
JVM之垃圾回收器下篇
MySQL字符串函数