SoFunction
Updated on 2025-03-08

Detailed explanation of G1 garbage collector tuning in concurrent scenarios

Preface

Currently, the Java version used by mainstream enterprise level is 8. The garbage collector supports manual modification to G1. The G1 garbage collector is the default setting of Java 11. Therefore, the G1 garbage collector can be used for a long time. At this stage, the garbage collector optimization means optimization for G1 garbage collector.

To simplify the discussion, the following assumes optimization for 4C/16G physical machines.

G1 Overview

1. Maximum heap size

The maximum heap size managed by G1 is 64G. The size of each region passes-XX:G1HeapRegionSizeTo set it, the size is 1~32MB, and the default can have up to 2048 Regions. The maximum heap memory that G1 can manage is 32MB*2048=64G.

The minimum heap memory used by G1 garbage collector should be 1MB*2048=2GB. It is recommended to use other garbage collectors below this value.

2. Region size

The Region size is 1~32MB, and the specific values ​​are 1MB, 2MB, 4MB, 8MB, 16MB, and 32MB. Region size optimization is related to large objects. When the object occupies more than half of the Region, it will be regarded as a large object.

Being marked as a large object will be detrimental to garbage collection.

3. Get the default value

Check out the current default values ​​of the local JVM, especially the G1 garbage collector.

java -XX:+PrintFlagsInitial  >> ~/

Three GC modes

G1 garbage collector has two garbage collection modes, new generation recycling and hybrid recycling, and will switch toFull GC

1. New generation recycling

The new generation recycling will process all garbage in the Eden area within the maximum pause time. The specific operation is to copy all surviving objects in the Eden area to the Survivor area and clear the Eden area at the same time.

The recycling of the new generation is accompanied by the application pause, and the maximum pause time does not exceed the maximum pause time. Although the new generation recycling has a pause mechanism, considering the characteristics of parallel recycling, the recycling logic is relatively simple and the recycling efficiency is still high. Generally speaking, the actual time taken by the Cenozoic recycling is usually lower than the maximum pause time.

The triggering time for the new generation of recycling is that the newly created object cannot find enough storage space in the Eden area.

2. Mixed recycling

Mixed recycling is accompanied by the new generation recycling and the elderly recycling. Within the maximum pause time, most of the garbage in the Eden area and some elderly garbage will be disposed of.

Older recycling will undoubtedly be accompanied by application pauses. Mixed recycling operations are relatively complicated. Compared with the new generation of recycling, the amount of garbage collected per unit time is less and the recycling efficiency is low. Generally speaking, the actual time taken for mixed recycling is usually close to or equal to the maximum pause time.

The hybrid recovery triggering time is the parameterInitiatingHeapOccupancyPercentControl, the default value is 45, which means that the ratio of the space occupied by the elderly to the total size of the heap exceeds this number and will trigger mixed recycling.

The default value of 45% is relatively reasonable, and the so-called tuning is not recommended. The elderly recycling strategy is also to copy the surviving objects in the selected region to the idle Region area. Mixed recycling can clean up a larger idle Region area to store surviving objects in the elderly area to ensure that the recycling process can proceed normally.

There are generally more surviving objects in the elderly area, and the replication of objects in memory takes longer, so the hybrid recycling efficiency is relatively low.

3、Full GC

Full GC is a situation that all G1 garbage collection tuners try their best to avoid. Single-threaded garbage recycling, and the recycling object is the entire heap and is no longer subject to the longest pause time. Once this happens, it means that the application's response time will ruthlessly grow longer.

When the application enters regularlyFull GCIn state, instead of allowing it to reshape the heap memory by a single thread, it is better to adopt a redundant strategy to restart the application one by one at the time of traffic trough and actively reshape the heap memory space.

The Full GC phenomenon occurs during peak traffic and its response strategies will be discussed later.

Default parameters

1. Heap memory

parameter default value illustrate Optimization suggestions
MaxGCPauseMillis 200ms Maximum pause time  
G1HeapRegionSize   Heuristic inference when not set  
G1NewSizePercent 5 Minimum percentage of the new generation  
G1MaxNewSizePercent 60 The maximum percentage of the new generation  

2. New generation memory recycling

parameter default value illustrate Optimization suggestions
ParallelGCThreads   The number of parallel GC threads will be inferred based on the number of CPU cores. default value
MaxTenuringThreshold 15 From the new generation to the age threshold for the elderly  
SurvivorRatio 8 Eden and a Survivor ratio  
TargetSurvivorRatio 50 Survivor area memory usage rate, increasing this value will reduce the probability of the elderly  
+G1EagerReclaimHumongousObjects true Whether to recycle large objects in YGC  

3. Mixed recycling

parameter default value illustrate Optimization suggestions
G1MixedGCCountTarget 8 The larger the value, the fewer the old age partitions are collected  
G1OldCSetRegionThresholdPercent 10 It means that up to 10% of partitions are collected at a time  

Garbage flows in the heap

The key to garbage collector tuning is to minimize the frequency of Mixed GCs as much as possible, in other words, minimize the amount of garbage flow toOlder age. GC tuning is to understand the flow of garbage in the heap, so as to interfere in advance on the garbage flowing to the elderly, so that it can be kept as much as possible.New generation

In the new generation (mainly referring to the Eden area), garbage collection uses YGC, and the recycling thread and the application thread are carried out concurrently. Garbage collection is transparent to the application. If the CPU computing power is sufficient, the application almost does not feel that the garbage is being recycled.

In the old age, garbage collection uses Mixed GC. When the recycling thread starts working, the application thread blocks, waiting for the recycling thread to complete, and the application thread is awakened again. Frequent Mixed GCs have adverse effects on application throughput.

1. How do the subject enter the old age

Generally speaking, newly created objects will exist in the Eden area of ​​the new generation, and the next garbage collection penalty will be directly recycled. If the object is more tenacious (continue to be referenced by other objects), it will flow in the Survivor area, once every GC, and it still cannot be garbage collected. Then add one to the age and continue to flow in the S0 and S1 areas. When the age increases to a certain threshold, it will directly enter the old age.

(1) Large objects go directly to the old age

If the newly created object is too large, it will directly enter the old age without going through the new generation. The control object size threshold has parameters -XX:PretenureSizeThreshold determines, unit bytes.

(2) Dynamic age judgment

In addition to the repeated age changes in the objects in S0 and S1 areas, garbage recycling maintains another set of independent age determination rules: if the garbage that has not been recycled after YGC exceeds 50% of the Survivor area, then the more than one group of objects will directly enter the elderly.

12G * 60% * 10% * 50% * 1024 = 737MB

The dynamic age determination rules require that each YGC be as thorough as possible, which means that the maximum time of each GC cannot be too short. The default 200 milliseconds is a relatively reasonable value.

If the preset maximum pause time is too short, then a large amount of garbage that has not been recycled has survived each time after GC, and the capacity of the S area is limited. The garbage that should not enter the elderly quickly accumulates in the elderly. Frequent Mixed GCs are inevitable.

2. High concurrency accelerates the entry into the old age

In high concurrency scenarios, CPU and memory resources are tight, the load is very high, and uncertain performance jitter accelerates the entry of garbage into the old age.

For example, the DAO layer querys the database. After a complete session is over, the object garbage generated in the entire session should be completely recycled in the Eden area. Due to network fluctuations and limitations in database processing capabilities, a large number of sessions timed out. During this process, this part of the object garbage may be superimposed on the age in the rapid S0 and S1 circulation, or trigger a dynamic age determination and directly enter the old age.

The memory space in the elderly is not enough, which triggers Mixed GC. The direct side effect of Mixed GC is the application lag.

Tuning steps

1. Set up a garbage collector

Java 8 requires manual specification of the G1 garbage collector, and add the -XX:+UseG1GC parameter in the command line.

2. Set the heap size

There are two points to note when setting the memory heap size: the initial heap size is consistent with the maximum heap size; the heap size accounts for 75% to 80% of the physical memory size, and reserves necessary memory for the system core services.

parameter-Xmx12GSet the initial heap size; parameters-Xms12GSet the maximum heap size.

3. Metaspace settings

Metaspace refers to the memory area that stores special variables such as static classes, static methods, constants, etc.

Parameter -XX:MetaspaceSize=1G sets the initial size of the metaspace; Parameter -XX:MaxMetaspaceSize=1G sets the maximum size of the metaspace.

4. GC pause time

GC pause time refers to each timeYGCorMixed GCThe maximum time of the garbage collector will dynamically select the range of garbage scanning based on the expected time set by the user. If the setting time is too small, there may always be some garbage that cannot be recycled. Units of milliseconds.

-XX:MaxGCPauseMillis=200

5. The size of the new generation

Parameter -XX:G1NewSizePercent sets the initial size of the new generation, default is 5%; parameter -XX:G1MaxNewSizePercent sets the maximum size of the new generation, default is 60%.

The new generation is refined intoEdenDistrict and twoSurvivor, the default ratio is: 8:1:1

Eden:   12G * 60%* 80% = 5.76G
S0:     12G * 60%* 10% = 0.72G
S1:     12G * 60%* 10% = 0.72G

Assuming that the concurrent system creates 500MB of objects per second, assuming that each YGC can be scanned to the Eden Region based on the preset maximum pause time, then this concurrent system needs to perform YGC every 10 seconds.

Tuning practice

GC garbage collection tuning is carried out under the limitation of physical hardware and the theoretical space for tuning. If conditions permit, directly upgrading the hardware configuration, especially the physical memory configuration, can effectively reduce the GC frequency. For example, 8C32G or 16C64G.

1. Frequent YGC

When the concurrency is large, frequent YGC is inevitable. More objects are created per unit time class, and it becomes garbage after use. Frequent YGCs have the possibility of accelerating the flow of objects in the S area to the elderly, and ensure that the actual time of each YGC is lower than the preset maximum garbage collection time (default 200 milliseconds), so that the new generation of garbage can be cleaned up every time and delay the flow of garbage to the elderly as much as possible.

2. Frequent Mixed GC

In the G1 garbage collector, there is no so-called Mixed GC concept. Mixed GC is similar to F·GC. The difference is that in addition to recycling the old age, Mixed GC also recycles the new generation. The common point is that they all produceSTW

FrequentMixed GCThe essence is that a large amount of garbage that should be recycled in the new generation has entered the old age. The solution is to check which garbage (objects) should be left in the new generation but transferred to the old age.

(1) Large object

Check whether the application periodically creates large objects. The threshold value of large objects is controlled by the parameter -XX:PretenureSizeThreshold. If the memory has room for optimization, the value should not be increased by more than half of the S area (it seems not to be such a large object). The side effect is that the number of objects stored in the new generation will be reduced accordingly, the memory in the Eden area will be used up faster, and the YGC will be changed more frequently accordingly.

From a business perspective, there must be reasons for the generation of large objects. From this perspective, the possibility of optimization is not high. The garbage collector optimization blocks the business layer code as much as possible. After all, it is unrealistic to set requirements for development to prevent them from creating large objects.

(2) Metaspace

Metaspace exhaustion will also causeMixed GC, considering the particularity of metaspace storage content, there is no good way to increase the GC frequency due to metaspace exhaustion. Simply increasing the size of the metaspace will compress the size of the Ceponymous generation. The Ceponymous generation will become smaller, and the number of objects transferred to the elderly will increase. The memory consumption of the elderly will accelerate, which will also increase the frequency of GC.

Caused by exhaustion of metaspaceMixed GC, relatively speaking, increasing physical memory is a relatively better solution.

3、Full GC

althoughMixed GCWhen triggered, the application temporarily stops responding (the default value is 200 milliseconds), and the pause time is relatively controllable.

If in progressMixed GCWhen the idle Region cannot save the surviving object, and when Mixed GC cannot proceed normally, garbage collection will switch to outside G1.Serial Old GCCome to collect the entire pile, including the new generation, the old generation, the Yuan space, etc.

EnterSerial Old GCGarbage recycling state, garbage collection is no longer subject to the maximum recycling time constraints. Single threads are used for marking, cleaning and compression. Applications may entersuspended animationstate. Maybe it would be better if you restart the application, reallocate the heap memory, and completely shuffle the heap memory.

The key to G1 garbage collection tuning is to avoid Full GC, so don’t tuning sensitive parameters randomly, otherwise it will not only fail to achieve ideals, but it will be even worse.

The above is the detailed explanation of the G1 garbage collector in concurrent scenario tuning. For more information about the concurrent tuning of the G1 garbage collector, please pay attention to my other related articles!