0

Note, this is not about GC

When I am doing a performance test on a SpringBoot application. I use JVisualVM to monitor the process and I curious to found it release memory to OS sometimes:

enter image description here

While monitoring an app created in my own framework, although there are huge gap between used heap size and the allocated memory, it still hold the memory instead of release some back to OS:

enter image description here

Does anyone know how can I force JVM to release memory back to OS if needed?

Gelin Luo
  • 14,035
  • 27
  • 86
  • 139
  • 3
    You cannot. Once the JVM has the memory allocated then it doesn't release it. If you want to the JVM to use less memory then, well, give it less. If you have given it 4GB then it will use up to 4GB. I honestly don't understand the issue. You can try hacking around with [`MaxHeapFreeRatio` and `MinHeapFreeRatio`](http://www.kubrynski.com/2014/08/runtime-commited-heap-resizing.html). – Boris the Spider Oct 28 '16 at 06:28
  • How is this question **not** about GC? – Kayaman Oct 28 '16 at 06:34
  • 1
    You'll notice that in both pictures `size` went down, and in both of the cases they didn't go below what was the maximum allocated size previously. In the second picture the memory usage almost went so high that memory would've been *increased*, so you can imagine the GC is a bit hesitant to start releasing memory in case there's another usage peak. The GC would eventually likely release memory in the latter case as well, provided you don't do any major allocations. – Kayaman Oct 28 '16 at 06:38
  • Thanks both of you guys. BoristheSpider I will try hack with the two options. Kayaman, your statement makes perfect sense to me. – Gelin Luo Oct 28 '16 at 06:42

0 Answers0