Runtime.getRuntime().freeMemory() with ZGC

Mike Rettig mike.rettig at
Tue Jun 1 14:59:11 UTC 2021

On Mon, May 31, 2021 at 6:19 AM Per Liden <per.liden at> wrote:

> Thanks for the feedback. What option would you have liked to have?
> Something like G1's MaxGCPauseMillis wouldn't make sense for ZGC, since
> pauses are O(1).
> /Per

When tuning GC, I look at three things.

1. Pause Duration - O(1) for ZGC
2. Pause Frequency
3. Memory Usage

I can control the memory usage with the min/max memory options but I don't
have a way to provide a target pause frequency.  For example, I could
specify this as MaxPausesPerMinute. ZGC can work to meet this goal (and
avoid allocation stalls) by growing/shrinking the heap accordingly.  This
makes GC tuning straightforward. If I want to optimize for memory use then
I can specify a very high pause rate, but if I want to avoid pauses then
I'll force ZGC to pause infrequently.
A typical app should just need to specify the max heap size and the target
pause frequency and that is it. ZGC can commit/uncommit memory as needed.

I prefer this approach over SoftMaxHeapSize because it is fully dynamic and
adapts to the app. Trying to specify exact memory thresholds is a
maintenance burden.  SoftMaxHeapSize assumes there is one optimal heap size
for the app. This is just not the case for many apps. At various times the
optimal heap size is dependent on the load at that time. For a website
there is probably a different setting for 2am, 1pm, and Cyber Monday.  This
leads developers to configure memory settings based on worst case scenarios
just as I have done.


More information about the zgc-dev mailing list