Search code examples
javagarbage-collectiong1gc

Consequences of setting G1HeapWastePercent to zero in G1


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 ?


Solution

  • 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:

    1. At the end of Only Young GC, it will be judged whether it is necessary to start concurrent marking
    2. After the concurrent marking is finish, the space occupied by live objects and garbage objects in each region (old generation) will be calculated.
      1. 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 the collection set.
      2. After that, sort the collection set, with the highest _gc_efficiency ranking first.
    3. If reclaimable_percent of collection set exceeds G1HeapWastePercent %, then Mixed GC will be triggered later.
    4. Mixed GC will select all young regions and old_region_length of old regions are reclaimed.
      1. At the end of Mixed GC execution, if reclaimable_percent still exceeds G1HeapWastePercent%, then Mixed GC will be executed again later.

    Let me explain several parameters and concepts mentioned below:

    G1MixedGCLiveThresholdPercent

    1. The default value in openjdk11 is 85, G1MixedGCLiveThresholdPercent
    2. 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.

    _gc_efficiency

    1. The recycling value of a single region
    2. _gc_efficiency = the size of garbage objects in a single region / the time it takes to transfer this region
    3. If there are more garbage objects in the region and the less time it takes to transfer, the _gc_efficiency will higher.

    G1HeapWastePercent

    1. The default value in openjdk11 is 5, G1HeapWastePercent
    2. The related code next_gc_should_be_mixed is used to determine whether the next GC is Mixed GC.

    reclaimable_percent

    1. reclaimable_percent = garbage object size / total space size
    2. The garbage object here refers to the total size of garbage objects in the collection set

    G1MixedGCCountTarget

    1. The default value in openjdk11 is 8, G1MixedGCCountTarget
    2. Indicates that once Mixed GC is triggered, Expect to execute 8 Mixed GC.

    G1OldCSetRegionThresholdPercent

    1. The default value in openjdk11 is 10, G1OldCSetRegionThresholdPercent
    2. Every time Mixed GC, the selected old_region_length has a maximum value
    3. The maximum value = the number of allocated regions in heap * 10 / 100

    old_region_length

    1. This value means that this Mixed GC selects old_region_length of Old Regions in collecion set for recycling
    2. The lower limit of this value is min_old_cset_length, and the upper limit is max_old_cset_length
    3. min_old_cset_length = the size of collection set / G1MixedGCCountTarget.
    4. max_old_cset_length = the number of allocated regions in heap * G1OldCSetRegionThresholdPercent / 100
    5. 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 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.