g1gc

Why XX:G1HeapRegionSize in G1GC must be power of 2?


Oracle documentation https://docs.oracle.com/javase/9/gctuning/garbage-first-garbage-collector.htm#JSGCT-GUID-98E80C82-24D8-41D4-BC39-B2583F04F1FF says that XX:G1HeapRegionSize must be power of 2 but there is no restriction in setting any value between 1m to 32m.

Question :

1.Can anyone explain why XX:G1HeapRegionSize must be power of 2 ?

2.For applications that deals with lots humongous objects, after extensive testing and analysing GC causes, GC throughput , GC pause time etc.,the setting XX:G1HeapRegionSize=10m seems to be appropriate. Is there any problem in setting 10m heap region for -Xmx12g ?


Solution

  • Can anyone explain why XX:G1HeapRegionSize must be power of 2 ?

    Simply because that is how Oracle has implemented the G1 collector.

    When implementing something like a garbage collector, you need to make design choices in order for it to work. There are probably a number of detailed reasons why the G1 designers made these particular choices. The general themes will be to optimize performance in critical G1 code paths (e.g. the write barriers) and/or to avoid unnecessary complexity in the G1 code base as a whole.

    (For example, if the size of a region is a power of two, and if each one starts on an address that is divisible by the region size, then you can use masking to efficiently compute which region an address belongs in. If you can save some instructions in the write barrier code, that could mean a couple of percentage points of performance for the entire JVM. I won't go into more detail ... but you can look up the literature.)

    But the reasons are moot. You either live with the G1 restriction, or you select a different garbage collector that is better for your use-case.

    Is there any problem in setting 10m heap region for -Xmx12g ?

    Well ... it won't work ... because of the restriction that you have already found.