14

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

My question is, if that additional memory will be given back by the JVM to the OS (e.g. for other processes) when there is no need for much memory anymore by my application. Currently my application seems to hold that memory forever, even when not needed anymore.

trincot
  • 317,000
  • 35
  • 244
  • 286
cretzel
  • 19,864
  • 19
  • 58
  • 71

3 Answers3

27

The JVM does return memory to the OS, but only very reluctantly, since it may need it 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 eagerly, you can use the tuning parameters of the Oracle JVM, specifically -XX:MaxHeapFreeRatio and -XX:MinHeapFreeRatio

Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720
3

Not necessarily. It depends on JVM you use. I know of one case, with IBM's JVM. We also had an issue with a program not freeing memory, even when it's not needed. Some info can be found on IBM's site.

darioo
  • 46,442
  • 10
  • 75
  • 103
0

It depends on the version indeed and on the garbage collectors selected. I found that java 1.8.0_73 does occasionally release small amounts back to the OS by default. This is the earliest version I remember seeing it without having to adjust JVM parameters, but it may very well apply to earlier ones, I don't know an exact version.

Unless parameters are adjusted, by default it likely won't release anything unless 60-70% of your heap is unused.

Herein may lie a performance consideration - java had a reputation for being slow, so by default the JVM may try to minimise allocation from the OS and making garbage collection as efficient as it can by holding on to a lot of memory and doing it less often.

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

nsandersen
  • 896
  • 2
  • 16
  • 41