My application sees occasional spikes in memory usage. After these rare spikes, I want to return unused heap memory back to OS. So I am playing with following JVM options:
However what I am seeing is that even though more than 40% heap is free, heap size is not shrinking and memory is not going back to OS. This is what I see with XX:printGCDetails:
[GC [DefNew: 2149K->122K(2304K), 0.0012210 secs] 18851K->16824K(31228K), 0.0012870 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[GC [DefNew: 2234K->25K(2304K), 0.0009530 secs] 18936K->16736K(31228K), 0.0010060 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
From above output: heap utlization = 16736/31228 = 54% (approx)
Which means 46% heap is free and should shrink since we specified MaxHeapFreeRatio=40, but its not shrinking.
Overall I am seeing my application using less memory than when I don't use MaxHeapFreeRatio, but why are these numbers not matching?
[url http://webcache.googleusercontent.com/search?q=cache:casRD1gOOPoJ:forums.sun.com/thread.jspa%3FthreadID%3D5413021%26start%3D0+site:forums.sun.com+tschodt+heap+release&cd=1&hl=en&ct=clnk#10847695]This old post ^[url http://webcache.googleusercontent.com/search?q=cache:casRD1gOOPoJ:forums.sun.com/thread.jspa%3FthreadID%3D5413021%26start%3D0+site:forums.sun.com+tschodt+heap+release&cd=1&hl=en&ct=clnk#10847695]Google cache^ might enlighten you.
So this memory becomes available for other applications running on that system.
Modern desktops use a virtual memory system for any normal application (excluding things that are part of the OS itself.)
There is no "memory" (in your terms of ram.)
All there is is addressable space.
The OS manages that for all OSes by mapping it from the ram to the applications, all of them, as needed.
The problem is if your application is actively using the memory and the the processing of swapping the memory strains the load on the computer itself for your application or other applications.
So it sounds like if my application has excess unused heap space, it will be swapped out if OS sees more demand for memory. I have verified this by writing another memory hog test process to create this demand.
But what I see is that when there is more demand for memory, first hit is taken by (disk) buffers and cached memory. Only after this buffers and cached memory has shrunk significantly, do I see my application's resident memory size going down. Not sure if this is good or bad. I think it would be nice if I could influence freeing up excess heap memory from my application back to OS.