Does the JVM provide free memory for the OS when it is no longer needed?

I have an application that temporarily requires a certain amount of memory / heap for some processes. Given the reasonable maximum heap size for the JVM as an option, the JVM starts with a small heap and requests more memory from the OS on demand.

My question is if this extra memory will be returned by the JVM to the OS (e.g. for other processes) when my application no longer needs a lot of memory. Currently, my application seems to retain this memory forever, even if it is no longer needed.

+12
java heap memory jvm
Jan 07 2018-11-11T00:
source share
3 answers

The JVM returns memory to the OS, but only very reluctantly, as it may be needed again soon, and getting memory from the OS is a relatively expensive operation.

If you want the JVM to return memory to the OS more quickly, you can use the Oracle JVM settings, in particular -XX:MaxHeapFreeRatio and -XX:MinHeapFreeRatio

+22
Jan 07 2018-11-11T00:
source share

Not necessary. It depends on the JVM you are using. I know one case with the IBM JVM. We also had a problem with a program that did not free memory, even when it was not needed. Some information can be found on the IBM website .

+3
Jan 07 2018-11-11T00:
source share

It depends on the version and on the garbage collectors. I found that java 1.8.0_73 sometimes releases small amounts back to the OS by default. This is the earliest version that I remember seeing it without the need to configure JVM parameters, but it can be applied very well to earlier ones, I do not know the exact version.

If the parameters are not configured, by default it will probably not release anything if 60-70% of your heap is not used.

This may lead to an opinion about performance - java had a reputation for being slow, so by default, the JVM may try to minimize the allocation from the OS and make garbage collection as efficient as possible, holding onto large memory and making it less common.

I wonder if it could be less mean with memory if the OS has relatively little free memory left.?

0
Jan 12 '17 at 14:09 on
source share



All Articles