Friday 27 May 2016

Unused java memory is not released to OS

Unused java memory is not released to OS

Unused java heap memory is not getting released to OS even if you use the following VM options: -XX:MinHeapFreeRatio=10 -XX:MaxHeapFreeRatio=10 . Because any memory allocated to the JVM by the OS is only released back to the OS when the JVM exits. The garbage collector only make memory re-usable by the JVM and not by the OS. 

Under some circumstances, the GC will release memory back to the OS of its own accord, but I'm not aware of any JVM that allows an application to tell the GC to do this. And on top, the GC is rather conservative about doing this because, as a general rule, the JVM will operate more efficiently with more memory, and continually requesting / giving back memory to the OS is inefficient.

Though the serial garbage collector (-XX:+UseSerialGC) of the JVM can occasionally shrink the heap and return memory, it's almost never used in production for an obvious performance reason.