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

**URL:** https://discuss.elastic.co/t/jvm-very-greedy-with-memory-how-do-i-get-it-to-shrink-when-possible/348657
**Category:** Elasticsearch
**Created:** [December 5, 2023, 3:26pm UTC](https://discuss.elastic.co/t/jvm-very-greedy-with-memory-how-do-i-get-it-to-shrink-when-possible/348657 "2023-12-05T15:26:14Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Vulume](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vulume/32/128688_2.png) [@Vulume](https://discuss.elastic.co/u/Vulume)
#### Post date: [December 5, 2023, 3:26pm UTC](https://discuss.elastic.co/t/jvm-very-greedy-with-memory-how-do-i-get-it-to-shrink-when-possible/348657/1 "2023-12-05T15:26:14Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Wolfram\_Haussig](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wolfram_haussig/32/70528_2.png) [@Wolfram\_Haussig](https://discuss.elastic.co/u/Wolfram_Haussig)
#### Post date: [December 6, 2023, 7:32am UTC](https://discuss.elastic.co/t/jvm-very-greedy-with-memory-how-do-i-get-it-to-shrink-when-possible/348657/2 "2023-12-06T07:32:18Z")

</div>

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](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

---

<div class="post-metadata">

### Author: ![Vulume](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vulume/32/128688_2.png) [@Vulume](https://discuss.elastic.co/u/Vulume)
#### Post date: [December 6, 2023, 1:57pm UTC](https://discuss.elastic.co/t/jvm-very-greedy-with-memory-how-do-i-get-it-to-shrink-when-possible/348657/3 "2023-12-06T13:57:37Z")

</div>

Thanks heaps!

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

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [January 3, 2024, 1:58pm UTC](https://discuss.elastic.co/t/jvm-very-greedy-with-memory-how-do-i-get-it-to-shrink-when-possible/348657/4 "2024-01-03T13:58:30Z")

</div>

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