当前位置:网站首页>Garbage collector driving everything -- G1
Garbage collector driving everything -- G1
2022-06-27 19:20:00 【User 3147702】
1. introduction
In the last article , We introduced CMS The specific recycling process of the garbage collection mechanism :
CMS The seven stages of implementation
We see ,CMS Under the garbage collection mechanism of , You want to tune the performance , Strong patience and rich experience are essential , Because the whole recycling process is related to jvm There are dozens of parameters , How to make CMS Adjusting the recycling mechanism to the most suitable use in the current scenario is a lot of trouble java A big problem for programmers .
For the optimization of the above problems ,G1 The garbage collector was born , It aims to enable developers to achieve system performance tuning through simple parameters :
-XX:+UseG1GC -Xmx32g -XX:MaxGCPauseMillis=200
- -XX:+UseG1GC Is a must , It is used to tell jvm Turn on G1 Garbage collector
- -Xmx32g Set the maximum heap memory size
- -XX:MaxGCPauseMillis Used to set the maximum pause time
After this configuration , You can use it G1 The garbage collector , Compared with CMS Isn't it easy ?
because G1 Have a prediction mechanism for garbage collection time , Therefore, it can ensure that the garbage collection is completed within the maximum pause time you set , This is sort of G1 The most amazing feature of the garbage collector .
Of course , In addition to the above three parameters , There are also several optional parameters for developers to configure , But compared to the CMS, It's still definitely easier to tune .
that ,G1 How is the garbage collector implemented ? Compared with CMS What advantages and disadvantages does he have ? This article will introduce in detail .
2. G1 And memory partition
G1 The first chapter of the garbage collector paper stay 2004 In published , To 2012 Year end was added to jdk 1.7u4 in , here we are jdk9,G1 Has become the default garbage collector , You can refer to the official documents :
https://docs.oracle.com/javase/9/gctuning/garbage-first-garbage-collector.htm#JSGCT-GUID-ED3AB6D3-FD9B-4447-9EDF-983ED2F7A573
Different from the traditional generation garbage collector ,G1 The heap memory is no longer divided into the preset young generation and the old generation , Instead, the heap memory is divided into several discontinuous areas of the same size -- Region, Every Region Occupy a contiguous virtual memory address space .
As shown in the figure ,Region There are four types :
- Eden District -- The new generation
- Survive District -- Survival zone
- Old District -- Old age
- Humongous District -- Huge object store
The new generation 、 Survival zone 、 The definition of old age and CMS There is no obvious difference in , but G1 Added in Humongous Storage area , This space is used to store more than Region An oversized object half the size .
By default ,G1 Each... Is automatically set according to the size of the heap memory Region Size , We can also set one through the following parameters Region Size , The value range is 1M To 32M, And it must be 2 The index of :
-XX:G1HeapRegionSize=2M
3. Object allocation strategy
Now that we know G1 Memory partition under management , that , If you want to create a new object , Which memory area will it be allocated to ?
G1 There are three levels of object assignment :
- TLAB(Thread Local Allocation Buffer) Thread local allocation buffer
- stay Eden District Distribution
- stay Humongous District Distribution
stay Eden In the space ,G1 Each allocation thread is assigned a separate ThreadLocal Space , This can effectively avoid synchronization problems in the concurrent process , This ensures faster object allocation .
If TLAB Space in cannot be allocated , Then you will try to Eden Allocate in space , That is to try in a certain Region Distribute within .
If the size of the object exceeds Region Half the size , that , Only in Humongous Space is allocated .
4. G1 Recycling process
Just like other garbage collectors before ,G1 The recycler is also divided into pairs Eden Area Young GC And the elderly generation Of Mix GC.
4.1 G1 Medium RSet And Card Table
Only for the new generation or the elderly generation GC, Obviously , You cannot scan only the corresponding partition , Because there may be cases where objects of old age refer to objects of new generation or objects of new generation refer to objects of old age , In this case , No matter which partition is reclaimed , The entire heap memory must be scanned , This is obviously too time-consuming and unacceptable .
As we are introducing CMS Process ,CMS adopt RSet and Card Table Realize the marking of the above situation ,G1 A similar strategy is used .
RSet Namely RememberedSet, He followed the leap heap A reference to a field , And CMS contrary ,G1 There is no record of which partitions are referenced by the current partition , But in RSet It records which partitions refer to the objects of the current partition , This is because G1 Adopt a segmented Region Too many partitions , meanwhile , An object may reference many objects , If you record the partition referenced by the current partition , The scanning range will still be large .
however , Just record which partitions refer to the current partition , And scan the objects in the partition that refer to the current partition , The cost of scanning is still high , therefore G1 Introduced Card Table, and CMS In the same , One Card Table Will a Region Divided into 128 To 512 A number of bytes Card, By marking Card Is it dirty and referenced , The above scanning process can be further refined .
4.2 SATB
G1 As a whole, the recycling process is divided into marking process and recycling process , Because the marking process is concurrent , and CMS equally , A new missing label object will be generated during the marking process , This may cause the object to be incorrectly recycled .
To avoid this happening ,G1 Introduced SATB, He is Snapshot-At-The-Beginning Abbreviation .
SATB The mechanism maintains a two-way linked list , Point to each newly assigned object , In this way, the garbage collector can easily know at the beginning GC Newly allocated objects after , So as to avoid objects being recycled by mistake , But this will cause new garbage objects generated in the recycling process to not be recycled , cause float garbage.
4.3 Young GC
Based on the above data structure ,Young GC The main workflow of is :
- Mark -- Scan static and local objects
- Handle dirty card, to update RSet
- Detect objects from the younger generation to the older generation
- Object Copy
- Handle soft quotes 、 Weak and virtual references
in fact , Recycling focuses on the process of copying objects , In the process ,G1 take Eden The active objects of the zone are copied to survive District , If survive District full , Will be promoted directly to old District ,survive Objects in the zone that have not been recycled for many times will also be promoted to old District .
4.4 Mix GC
Mix GC It is used to recycle the old generation , He has two steps :
- Global concurrency token
- Copy live objects
The global concurrency marking is divided into five stages :
- Initial marker -- Yes GC Roots marked , need Stop The World
- Root area scan -- At the initial mark of survival Region Scan for references to old times , And mark the referenced object
- Concurrent Tags -- Mark live objects throughout the heap , interruptible
- Final marker -- Empty SATB buffer , Tracking live objects that are not accessed , This stage requires Stop The World
- Clear away rubbish -- That is, the concurrent cleanup phase , In execution GC Statistics and purification RSet In the process Stop The World
4.5 Full GC
Due to Division Region, Give Way G1 It is less likely to generate memory fragments , But anyway , Memory fragments will still follow jvm The work of .
stay Mix GC Before , The old age has been filled , perhaps Young GC Memory allocation failed in the process of upgrading to the old age , Or failed to allocate a jumbo object , Will trigger serialization Full GC
5. G1 The advantages and disadvantages of
G1 The biggest advantage is to discretize the heap memory space , So as to improve the efficiency of object allocation and recycling , Reduce memory fragmentation .
meanwhile ,G1 The garbage collector also has a pause prediction model , So as to meet the requirements set by the user as much as possible GC Expected pause time , So as to make the whole recycling process more controllable .
however ,G1 The pause prediction function of does not accurately guarantee the recovery process within the set time range .
6. G1 Related parameters of
It says G1 Basic parameters used , You can see G1 The garbage collector does not need the user to configure cumbersome parameters to work well , But we can still tune the performance with limited parameters .
G1 Have the following parameters :
Parameters | meaning |
|---|---|
-XX:G1HeapRegionSize=n | Set up Region size , It's not the end result , Because in the end, we can only take 2 The power of |
-XX:MaxGCPauseMillis | Set up G1 Collection process target time , The default value is 200ms, It's not a hard condition |
-XX:G1NewSizePercent | New generation minimum , The default value is 5% |
-XX:G1MaxNewSizePercent | New generation maximum , The default value is 60% |
-XX:ParallelGCThreads | STW period , parallel GC Number of threads |
-XX:ConcGCThreads=n | Concurrent tagging phase , Number of threads executing in parallel |
-XX:InitiatingHeapOccupancyPercent | Set the Java Heap occupancy threshold . The default value is 45%. there java The heap ratio means non_young_capacity_bytes, Include old+humongous |
If there are many large objects , Can be Region Set it larger appropriately , This prevents the frequent allocation of jumbo objects .
ParallelGCThreads Parameters are generally not recommended 8 above , Unless the processor is more than 8 individual , It is recommended to set to the processor 5/8 about ,ConcGCThreads It is recommended to set to ParallelGCThreads Of 1/4, Prevent excessive use of worker thread resources .
appendix - Reference material
https://www.oracle.com/webfolder/technetwork/tutorials/obe/java/G1GettingStarted/index.html
http://dl.acm.org/citation.cfm?id=1029879
https://hllvm-group.iteye.com/group/topic/44381
https://hllvm-group.iteye.com/group/topic/44529
https://tech.meituan.com/2016/09/23/g1.html
http://ghoulich.xninja.org/2018/01/27/understanding-g1-garbage-collector-in-java/
边栏推荐
- [elt.zip] openharmony paper Club - memory compression for data intensive applications
- Summary of domestic database certification test guide (updated on June 16, 2022)
- Comment encapsuler un appel à une bibliothèque
- Informatics Olympiad 1333: [example 2-2] blah data set | openjudge noi 3.4 2729:blah data set
- How to use the low code platform of the Internet of things for picture management?
- 过关斩将,擒“指针”(下)
- 【建议收藏】ABAP随笔-EXCEL-4-批量导入-推荐
- Market status and development prospect forecast of global tetramethylammonium hydroxide developer industry in 2022
- maxwell 报错(连接为mysql 8.x)解决方法
- Keras deep learning practice (12) -- facial feature point detection
猜你喜欢
随机推荐
MFS分布式文件系统
工作流自动化 低代码是关键
Usage of rxjs mergemap
Market status and development prospect forecast of global 4-methyl-2-pentanone industry in 2022
Bit.Store:熊市漫漫,稳定Staking产品或成主旋律
Redis Series 2: data persistence improves availability
芯动联科冲刺科创板:年营收1.7亿 北方电子院与中城创投是股东
Stored procedures of PostgreSQL
[elt.zip] openharmony paper Club - memory compression for data intensive applications
Informatics Orsay all in one 1335: [example 2-4] connected block
The IPO of Yuchen Airlines was terminated: Guozheng was proposed to raise 500million yuan as the major shareholder
Making single test so simple -- initial experience of Spock framework
2022年信创行业空间测算
Blink SQL built in functions
云原生数据库:数据库的风口,你也可以起飞
International School of Digital Economics, South China Institute of technology 𞓜 unified Bert for few shot natural language understanding
教你打印自己的日志 -- 如何自定义 log4j2 各组件
SQL update批量更新
PostgreSQL数据库WAL——资源管理器RMGR
Alibaba's mission, vision and core values









