Runtime.getRuntime().freeMemory() with ZGC

Per Liden per.liden at oracle.com
Thu Jun 3 10:01:24 UTC 2021


On 6/1/21 4:59 PM, Mike Rettig wrote:
> 
> 
> On Mon, May 31, 2021 at 6:19 AM Per Liden <per.liden at oracle.com 
> <mailto:per.liden at oracle.com>> 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.

If I understand your use case correctly, it sounds like the option you 
are looking for is -XX:ZCollectionInterval=<seconds>. This tells ZGC to 
do a GC at least every <seconds> (where <seconds> can be floating point 
number). Since pauses are O(1), this effectively means the same thing as 
"target pause frequency" or "target accumulated pause time per minute" 
but specified in a different way. ZGC would only collect garbage more 
often than ZCollectionInterval if it has already grown the heap to its 
max size and it's still not enough to avoid an allocation stall. 
However, under "normal conditions" it will grow/shrink the heap to a 
size that is enough to meet the ZCollectionInterval goal.

/Per

> 
> 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.
> 
> Mike
> 


More information about the zgc-dev mailing list