Is there a way to lower Java heap when not in use?

You could perhaps play around with -XX:MaxHeapFreeRatio – this is the maximum percentage (default 70) of the heap that is free before the GC shrinks it. Perhaps setting it a bit lower (40 or 50?) and then using System.gc() might go some lengths to get you the desired behaviour?

There’s no way to force this to happen however, you can try and encourage the JVM to do so but you can’t just yank memory away as and when you want to. And while the above may shrink the heap, that memory won’t necessarily be handed straight back to the OS (though in recent implementations of the JVM it does.)

Leave a Comment