JVM very greedy with memory. How do I get it to shrink when possible?

I want my JVM to give back memory to the OS if it's not using it. I don't care about performance.

When I set -Xms128m -Xmx4g, I see the JVM's memory usage grow while indexing and searching, but it never shrinks again afterwards. For most of my projects it will grow to about 2GB.
I am positive that it doesn't need all that memory, because most of my projects run fine with -Xmx256m and JVM memory will remain at around 0.5GB.

It will predictably crash with the largest projects. I would like to set a large enough heap size to be safe, without unnecessarily paying >1GB for this.

Which ES settings or JVM flags can I use for this?

Using ES 7.17 on a Windows Server 2022 with the JVM included with ES.

Hi,

I have not tried it as it is generally not recommended, but you can try the guide provided here: https://www.baeldung.com/gc-release-memory

Basically, you would need to set -XX:MaxHeapFreeRatio to make the JVM free memory when the maximum is reached and -XX:-ShrinkHeapInSteps to false so the process of freeing RAM takes place immediately.

This should work as the included Java version is Java12+ and the jvm.options make use of the G1 garbage collector.

Best regards
Wolfram

Thanks heaps!

It seems I only need -XX:-ShrinkHeapInSteps for reasonable results.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.