Good day , I am using G1 in production(Hotspot JDK 11) and trying to understand how it works. As far as I understand , Old GC in G1 only cleans those regions which are full of garbage, otherwise regions are put into some queue.
Later on Mixed GC is triggered and it will clean Young regions with amount of Old generations/G1MixedGCCountTarget
Old regions. Old regions to be scanned are determined by G1MixedGCLiveThresholdPercent
which by default is 65% which is if 65 % of the region is garbage then it will be cleaned.
However I am not sure if I understood the meaning of G1HeapWastePercent
.
From the official documentation
Sets the percentage of heap that you are willing to waste. The Java >HotSpot VM does not initiate the mixed garbage collection cycle when the >reclaimable percentage is less than the heap waste percentage. The default is 10 percent. This setting is not available in Java HotSpot VM, build 23.
So let's say regions contains 65% of garbage. G1 started to move life objects from Young regions to single Old region that is filled with garbage, once this region only contains 10 % of the garbage then G1 switches to another old region region. If my assumption is correct then setting G1HeapWastePercent
to 0 will fully fill this region with life objects. Is it correct ? And will I have any benefits setting G1HeapWastePercent to zero if time latency isn't a priority for my application ?
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:
Only Young GC
, it will be judged whether it is necessary to start concurrent marking
concurrent marking
is finish, the space occupied by live objects and garbage objects in each region (old generation) will be calculated.
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 the collection set
.collection set
, with the highest _gc_efficiency
ranking first.reclaimable_percent
of collection set
exceeds G1HeapWastePercent
%, then Mixed GC
will be triggered later.Mixed GC
will select all young regions and old_region_length
of old regions are reclaimed.
reclaimable_percent
still exceeds G1HeapWastePercent
%, then Mixed GC
will be executed again later.Let me explain several parameters and concepts mentioned below:
collection set
.the size of garbage objects in a single region
/ the time it takes to transfer this region
Mixed GC
.garbage object size
/ total space size
collection set
the number of allocated regions in heap
* 10 / 100Mixed GC
selects old_region_length
of Old Regions in collecion set
for recyclingmin_old_cset_length
, and the upper limit is max_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
GC Pause Time
, so the final old_region_length is constrained by the gc pause time. But Mixed GC will put as many regions in collecion set
as possible into old_region_length
, until the total time it takes to reclaim these regions is close to the estimated GC Pause Time
.