Repository navigation
share jdk11_G1HeapWastePercent
首先,我认为您可能误解了 G1MixedGCLiveThresholdPercent 的作用(该参数的默认值在 Jdk7 是 65%,在 Jdk 11 是 85%)。 G1MixedGCLiveThresholdPercent 不是垃圾对象的空间占比,而是存活对象的空间占比。如果一个 region 内的存活对象的空间占比超过了 85% (in jdk11),那么该 region 不会进入 collection set (待回收的 region 集合)
Firstly,I think you may have misunderstood the effect of G1MixedGCLiveThresholdPercent (the default value is 65% in Jdk7 and 85% in Jdk 11).
G1MixedGCLiveThresholdPercent is not the space ratio of garbage objects, but the space ratio of live objects. If the space of live objects in a region exceeds 85% (in jdk11), then the region will not enter the collection set (region collection to be recycled)
其次,G1HeapWastePercent(该参数的默认值在 Jdk 7 是 10%,在 Jdk 11 是 5%)是触发 Mixed GC 和 退出 Mixed GC 的条件,当 collection set 内的垃圾对象占全部 heap 已分配空间的占比超过 5% (in jdk11)时会触发 Mixed GC,而退出 Mixed GC 的其中一个充分条件就是 collection set 内的垃圾对象率低于了 5%。
Secondly,G1HeapWastePercent (the default value is 10% in Jdk7 and 5% in Jdk11)
is a condition for triggering Mixed GC and exiting Mixed GC.
When the garbage objects in the collection set account for more than 5% (in jdk11) of the total heap allocated space,
the Mixed GC will be triggered, and one of the sufficient conditions for exiting the Mixed GC That is
the garbage object rate in the collection set is lower than 5%.
因此,如果把 G1HeapWastePercent 设置为 0%,那么只要 collection set 里面有垃圾对象,就会触发 Mixed GC。 然后退出 Mixed GC 的充分条件就会变为 collection set 为空, 因为每一次的 Mixed GC 都会从 collection set 中移除已回收的 region。 这导致的结果就是 Mixed GC 的触发频率非常高,也就使得进入 STW 的频率也会变高, mutator 线程就会被频繁打断,严重降低程序的性能。
Therefore, if G1HeapWastePercent is set to 0%, as long as there are garbage objects in the collection set, Mixed GC will be triggered.
Then the sufficient condition for exiting Mixed GC will become that the collection set is empty,
Because every Mixed GC will remove the reclaimed region from the collection set.
最后,我想和您解释下 G1 GC 的执行流程,让您可以更好的理解整个过程。
Finally, I want to explain to you the execution process of G1 GC, so that you can better understand the whole process.
G1 包含3个过程,并发标记、Only Young GC、Mixed GC。大致流程如下:
G1 contains 3 phases, concurrent marking, Only Young GC, and Mixed GC. The general process is as follows:
-
在 Only Young GC 结束的时刻会判断是否需要启动并发标记
-
并发标记结束后,会计算出老年代每个 region 的 存活对象空间占用和垃圾对象的空间占用。
- 之后产生一个待回收的老年代 region 集合,只有存活对象大小占 region 总大小的比例超过 G1MixedGCLiveThresholdPercent 的老年代 region 才会进入该待回收集合。
- 之后对待回收的老年代 region 集合进行排序,_gc_efficiency 高的排前面。
-
如果
collection set的 reclaimable_percent 超过 G1HeapWastePercent%,那么接下来会触发 Mixed GC。 -
Mixed GC 会选择全部的 young regions 和 old_region_length个 old regions 进行回收。
- 如果 Mixed GC 执行结束时,reclaimable_percent依然超过 G1HeapWastePercent%,那么接下来还会再次执行 Mixed GC,反之会退出 Mixed GC
-
At the end of
Only Young GC, it will be judged whether it is necessary to startconcurrent marking -
After the
concurrent markingis finish, the space occupied by live objects and garbage objects in each region (old generation) will be calculated.- Then the
collection set(old region collection to be recycled) is generated, and only the old generation regions whose size of surviving objects account for the total size of the region exceeds G1MixedGCLiveThresholdPercent will enter thecollection set. - After that, sort the
collection set, with the highest_gc_efficiencyranking first.
- Then the
-
If
reclaimable_percentofcollection setexceedsG1HeapWastePercent%, thenMixed GCwill be triggered later. -
Mixed GCwill select all young regions andold_region_lengthof old regions are reclaimed.- At the end of Mixed GC execution, if
reclaimable_percentstill exceedsG1HeapWastePercent%, thenMixed GCwill be executed again later.
- At the end of Mixed GC execution, if
下面解释一下提到的几个参数和概念:
Let me explain several parameters and concepts mentioned below:
-
在 openjdk11 中默认值是 85,G1MixedGCLiveThresholdPercent
-
相关的使用位置在 mixed_gc_live_threshold_bytes,作用是计算单个 region 的对象存活率的阈值,超过该阈值的 region 不会进入待待回收的老年代 region 集合
-
The default value in openjdk11 is 85, G1MixedGCLiveThresholdPercent
-
The relevant use location is mixed_gc_live_threshold_bytes, which is used to calculate the threshold of the survival rate of objects in a single region. Regions that exceed this threshold will not enter the
collection set.
-
单个 region 的回收价值
-
_gc_efficiency = 单个 region 内的垃圾对象大小 / 转移这个 region 需要花费的时间
-
如果 region 内的垃圾对象越多,转移花费的时间越少则 _gc_efficiency 越高
-
The recycling value of a single region
-
_gc_efficiency =
the size of garbage objects in a single region/the time it takes to transfer this region -
If there are more garbage objects in the region and the less time it takes to transfer, the _gc_efficiency will higher.
-
在 openjdk11 中的默认值是 5,G1HeapWastePercent
-
相关使用代码 next_gc_should_be_mixed,作用是判断下一次 GC 是否是 Mixed GC
-
The default value in openjdk11 is 5, G1HeapWastePercent
-
The related code next_gc_should_be_mixed is used to determine whether the next GC is
Mixed GC.
-
reclaimable_percent = 垃圾对象大小 / 总空间的大小
-
这里的垃圾对象是指待回收的老年代 region 集合中的垃圾对象大小总和
-
reclaimable_percent =
garbage object size/total space size -
The garbage object here refers to the total size of garbage objects in the
collection set
-
在 openjdk11 中的默认值是 8,G1MixedGCCountTarget
-
表示一旦触发 Mixed GC 之后,期望执行 8 次 Mixed GC。
-
The default value in openjdk11 is 8, G1MixedGCCountTarget
-
Indicates that once Mixed GC is triggered, Expect to execute 8 Mixed GC.
-
在 openjdk11 中的默认值是10,G1OldCSetRegionThresholdPercent
-
每次 Mixed GC 时,选择的 old_region_length 有个最大值
-
最大值 = 已分配的 region 个数 * 10 / 100
-
The default value in openjdk11 is 10, G1OldCSetRegionThresholdPercent
-
Every time Mixed GC, the selected old_region_length has a maximum value
-
The maximum value =
the number of allocated regions in heap* 10 / 100
-
这个值表示本次 Mixed GC 选择 old_region_length 个 Old Region 进行回收
-
old_region_length 的下限是 min_old_cset_length , 上限是 max_old_cset_length
-
min_old_cset_length= collection set size / G1MixedGCCountTarget. -
max_old_cset_length= 已分配的 region 个数 * G1OldCSetRegionThresholdPercent / 100 -
由于 G1 会严格控制每次 GC Pause 的时间,所以最终的 old_region_length 受暂停时间的约束。但 Mixed GC 会尽可能的放入更多的 old_region,直到回收这些 region 花费的总时间接近预计的 GC Pause Time。
-
This value means that this
Mixed GCselectsold_region_lengthof Old Regions incollecion setfor recycling -
The lower limit of this value is
min_old_cset_length, and the upper limit ismax_old_cset_length -
min_old_cset_length=the size of collection set / G1MixedGCCountTarget. -
max_old_cset_length=the number of allocated regions in heap * G1OldCSetRegionThresholdPercent / 100 -
Because G1 will strictly control each
GC Pause Time, so the final old_region_length is constrained by the gc pause time. But Mixed GC will put as many regions incollecion setas possible intoold_region_length, until the total time it takes to reclaim these regions is close to the estimatedGC Pause Time.
假设 G1MixedGCCountTarget = 8,G1OldCSetRegionThresholdPercent = 10。我给出了在不同参数下的 old_region_length min and max,以及理论上的 Mixed GC Count。
实际的 Mixed GC Count 会比表格中给出的值要小,因为当 reclaimable_percent 小于 G1HeapWastePercent 时,会退出 Mixed GC
-
min_old_cset_length=collection set/G1MixedGCCountTarget -
max_old_cset_length=Heap region num*G1OldCSetRegionThresholdPercent/100 -
old_region_length的值介于min_old_cset_length和max_old_cset_length -
Mixed GC Count=collection set/old_region_length
| Heap region num | collection set | min_old_cset_length | max_old_cset_length | old_region_length (min) | Mixed GC Count (min) | old_region_length (max) | Mixed GC Count (max) |
|---|---|---|---|---|---|---|---|
| 1024 | 128 | 16 (128/8) | 103 (1024 * 10 / 100 + 1) | 16 | 8 (128 / 16) | 103 | 2 (128 / 103 + 1) |
| 1024 | 512 | 64 (512/8) | 103 (1024 * 10 / 100 + 1) | 64 | 8 (512 / 64) | 103 | 5 (512 / 103 + 1) |
| 1024 | 824 | 103 (824/8) | 103 (1024 * 10 / 100 + 1) | 103 | 8 (824 / 103) | 103 | 8 (824 / 103) |
| 1024 | 1024 | 128 (1024/8) | 103 (1024 * 10 / 100 + 1) | 103 | 10 (1024 / 103 + 1) | 103 | 10 (1024 / 103 + 1) |
可以看到,当 collection set 的值小于 824 的时候,最大 Mixed GC 次数都是 8. 但是 collection set 的值超过 824 的时候,次数就会超过 8 且最大值为 10. 但是考虑 G1 在大部分场景中都会预留一部分 region 给 young generation,因此我解释是在绝大部分情况下不会超过 G1MixedGCCountTarget
Suppose G1MixedGCCountTarget = 8, G1OldCSetRegionThresholdPercent = 10. I gave old_region_length min and max under different parameters, and the theoretical value of Mixed GC Count.
The actual Mixed GC Count will be smaller than the value given in the table, because when reclaimable_percent is less than G1HeapWastePercent, Mixed GC will exit
-
min_old_cset_length=collection set/G1MixedGCCountTarget -
max_old_cset_length=Heap region num*G1OldCSetRegionThresholdPercent/100 -
old_region_lengthis betweenmin_old_cset_lengthandmax_old_cset_length -
Mixed GC Count=collection set/old_region_length
So I think G1MixedGCCountTarget represents the maximum number of Mixed GC
Firstly,I think you may have misunderstood the effect of G1MixedGCLiveThresholdPercent (the default value is 65% in Jdk7 and 85% in Jdk 11).
G1MixedGCLiveThresholdPercent is not the space ratio of garbage objects, but the space ratio of live objects. If the space of live objects in a region exceeds 85% (in jdk11), then the region will not be selected into the
collection set (region collection to be recycled)
Secondly,G1HeapWastePercent (the default value is 10% in Jdk7 and 5% in Jdk11)
is a condition for triggering Mixed GC and exiting Mixed GC.
When the garbage objects in the collection set account for more than 5% (in jdk11) of the total heap allocated space,
the Mixed GC will be triggered, and one of the sufficient conditions for exiting the Mixed GC That is
the garbage object rate in the collection set is lower than 5%.
Therefore, if G1HeapWastePercent is set to 0%, as long as there are garbage objects in the collection set, Mixed GC will be triggered.
Then the sufficient condition for exiting Mixed GC will become that the collection set is empty,
Because every Mixed GC will remove the reclaimed region from the collection set.
Finally, I want to explain to you the execution process of G1 GC, so that you can better understand the whole process.
G1 contains 3 phases, concurrent marking, Only Young GC, and Mixed GC. The general process is as follows:
- At the end of
Only Young GC, it will be judged whether it is necessary to startconcurrent marking - After the
concurrent markingis finish, the space occupied by live objects and garbage objects in each region (old generation) will be calculated.- Then the
collection set(old region collection to be recycled) is generated, and only the old generation regions whose size of surviving objects account for the total size of the region exceeds G1MixedGCLiveThresholdPercent will enter thecollection set. - After that, sort the
collection set, with the highest_gc_efficiencyranking first.
- Then the
- If
reclaimable_percentofcollection setexceedsG1HeapWastePercent%, thenMixed GCwill be triggered later. -
Mixed GCwill select all young regions andold_region_lengthof old regions are reclaimed.- At the end of Mixed GC execution, if
reclaimable_percentstill exceedsG1HeapWastePercent%, thenMixed GCwill be executed again later.
- At the end of Mixed GC execution, if
Let me explain several parameters and concepts mentioned below:
- The default value in openjdk11 is 85, G1MixedGCLiveThresholdPercent
- The relevant use location is mixed_gc_live_threshold_bytes, which is used to calculate the threshold of the survival rate of objects in a single region. Regions that exceed this threshold will not enter the
collection set.
- The recycling value of a single region
- _gc_efficiency =
the size of garbage objects in a single region/the time it takes to transfer this region - If there are more garbage objects in the region and the less time it takes to transfer, the _gc_efficiency will higher.
- The default value in openjdk11 is 5, G1HeapWastePercent
- The related code next_gc_should_be_mixed is used to determine whether the next GC is
Mixed GC.
- reclaimable_percent =
garbage object size/total space size - The garbage object here refers to the total size of garbage objects in the
collection set
- The default value in openjdk11 is 8, G1MixedGCCountTarget
- Indicates that once Mixed GC is triggered, Expect to execute 8 Mixed GC.
- The default value in openjdk11 is 10, G1OldCSetRegionThresholdPercent
- Every time Mixed GC, the selected old_region_length has a maximum value
- The maximum value =
the number of allocated regions in heap* 10 / 100
- This value means that this
Mixed GCselectsold_region_lengthof Old Regions incollecion setfor recycling - The lower limit of this value is
min_old_cset_length, and the upper limit ismax_old_cset_length -
min_old_cset_length=the size of collection set / G1MixedGCCountTarget. -
max_old_cset_length=the number of allocated regions in heap * G1OldCSetRegionThresholdPercent / 100 - Because G1 will strictly control each
GC Pause Time, so the final old_region_length is constrained by the gc pause time. But Mixed GC will put as many regions incollecion setas possible intoold_region_length, until the total time it takes to reclaim these regions is close to the estimatedGC Pause Time.