Mykhaylo Adamovych
Mykhaylo Adamovych

Reputation: 20966

Server JVM not to retain memory

One of the differences between server and client JVM is that server JVM will try to use and retain all available memory (up to configured or default Xmx), but client JVM is 'merciful' to user machine and will not retain unused memory (source?).
On machine with large amount of memory and several CPUs 'server' JVM will be chousen by default, resulting in quite big default Xmx and 'server memory retantion policy'.
This will cause small java application slowly grabbing 4G of memory when in practice it needs only 100M.
How could I change 'server JVM memory retantion policy' to act like 'client' JVM, but not downgrade other 'server JVM performance improvements'?

Upvotes: 0

Views: 66

Answers (1)

markusk
markusk

Reputation: 6667

Assuming HotSpot JVM, Java 9. You can set a hard limit for heap size with -Xmx, or instruct the JVM to return memory to the OS if a sufficient ratio of the heap is unused with -XX:MaxHeapFreeRatio (in which case you should also set -XX:MinHeapFreeRatio).

The section Performance Tuning Examples in the Java 9 docs recommends

-XX:MaxHeapFreeRatio=10 -XX:MinHeapFreeRatio=5

for keeping the heap small.

Upvotes: 2

Related Questions