Min vs Max vs Soft Memory Limits

Per Liden per.liden at oracle.com
Mon May 10 08:04:22 UTC 2021


On 5/7/21 6:04 PM, Mike Rettig wrote:
> I've been using ZGC with JDK 16 for a few months now and have been pleased
> with the performance. I converted a few apps from G1 to ZGC so the heap
> could be increased without increasing pause times. ZGC has performed well
> in these scenarios.

Good to hear!

> I've recently been modifying the ZGC options to optimize performance and
> minimize the amount of vm option tweaking required for various
> environments.  The apps are deployed to a large range of servers with
> different amounts of memory.  The apps can be greedy about using memory so
> I don't have to worry about uncommitting memory. However, I'd like the ZGC
> collector to find an optimal state without giving it all the system memory
> at startup.

ZGC will typically try to use all the memory you give it, to space out 
GC cycles as much as possible, and thereby interfere as little as 
possible. So, ZGC will not really try to find the optimal state (in 
terms of memory usage) you're looking for here.

> Here is an example config:
> -Xms1024m -XX:SoftMaxHeapSize=1024m -XX:-ZUncommit
> I know the app needs at least 1gb. I don't set the max heap because all the
> servers are different. The jvm will default the max heap to 25% of the
> system memory. I originally didn't set the soft max heap, but I was
> surprised when it defaulted to the max heap. How does ZGC handle the soft
> heap in this configuration? If the app starts at 1gb but soon needs 2gb
> does the soft heap grow with it or does zgc still try to stay below 1gb
> since that was the initial value?

The SoftMaxHeapSize will stay at 1G, and if the heap grows to 2G, then 
ZGC will start doing GCs more often in order to try to get the heap 
usage down to 1G again.

> Would it be better not to set the soft heap and let it default to the max heap?

In your case, I would recommend that you set Xmx to more than what your 
app typically needs, and then set SoftMaxHeapSize to what that your app 
works well with, and so that GC cycles happen at some reasonable 
frequency. I would also recommend that you leave uncommit enabled. That 
way ZGC can grow the heap if there's a workload spike, but it will later 
return than memory once the spike is over.

> Does the jvm grow the heap
> differently when the soft heap is set to the min heap instead of the max
> heap when uncommit is disabled?

The way the heap grows will not be affected, SoftMaxHeapSize only 
affects how often GCs happens.

I wrote a blog post about SoftMaxHeapSize some time ago. I'm not sure 
the example I described there matches your use case, but you might find 
it useful nevertheless. https://malloc.se/blog/zgc-softmaxheapsize


> Mike

More information about the zgc-dev mailing list