Thalwander
Thalwander

Reputation: 21

Return memory from JVM to OS

I have a Java application which allocates lots of memory during the first minute after start up. Then it runs for 30 more minutes, but requires almost no memory. I want to return the unused memory to the OS.

I profiled the application, and it allocates about 200 MB in the first minute. Then 190 MB is garbage collected, but the process still hangs on to its 200 MB until the process is terminated. It never returns any memory to the operating system (Linux in my case), except for a puny couple of MBs.

How can I force the JVM to return unused memory back to the OS?

Several people has already asked the same question here at stackoverflow. The usual answer is to use JVM options -XX:MinHeapFreeRatio=x and -XX:MaxHeapFreeRatio=y. I have tried several different value combinations, but they make no difference.

This is how these options work, as I have understood it: If there is more free memory than MaxHeapFreeRatio after garbage collection, the JVM might return memory to the OS. For some reason my JVM does not seem to want to do this. I have tried different garbage collectors (-XX:+UseParallelGC etc) without success.

Upvotes: 2

Views: 590

Answers (1)

Stephen C
Stephen C

Reputation: 718768

You can't force the JVM to give memory back. The settings are advisory.

Also you may find that the JVM needs to perfom one or more full garbage collections before it decides that it can give back memory.

Upvotes: 1

Related Questions