Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the JVM give back free memory to the OS when no longer needed?

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.

like image 888
cretzel Avatar asked Jan 07 '11 11:01

cretzel


2 Answers

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

like image 114
Michael Borgwardt Avatar answered Sep 22 '22 06:09

Michael Borgwardt


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.

like image 22
darioo Avatar answered Sep 22 '22 06:09

darioo