# How to avoid incomplete garbage collection by G1GC causing high cpu usage on Elasticsearch nodes?

**URL:** https://discuss.elastic.co/t/how-to-avoid-incomplete-garbage-collection-by-g1gc-causing-high-cpu-usage-on-elasticsearch-nodes/357032
**Category:** Elasticsearch
**Created:** [April 9, 2024, 6:51am UTC](https://discuss.elastic.co/t/how-to-avoid-incomplete-garbage-collection-by-g1gc-causing-high-cpu-usage-on-elasticsearch-nodes/357032 "2024-04-09T06:51:41Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![wangxr1985](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wangxr1985/32/117798_2.png) [@wangxr1985](https://discuss.elastic.co/u/wangxr1985)
#### Post date: [April 9, 2024, 6:51am UTC](https://discuss.elastic.co/t/how-to-avoid-incomplete-garbage-collection-by-g1gc-causing-high-cpu-usage-on-elasticsearch-nodes/357032/1 "2024-04-09T06:51:41Z")

</div>

This was an issue I encountered before:

> [@Only one of the data nodes has a significantly higher cpu usage than other data nodes](https://discuss.elastic.co/t/only-one-of-the-data-nodes-has-a-significantly-higher-cpu-usage-than-other-data-nodes/326589):
>
> ES version: elasticsearch-5.6.3-1.noarch OS version: CentOS Linux release 7.6.1810 (Core) Linux version 3.10.0-1160.31.1.el7.x86\_64 (mockbuild@kbuilder.bsys.centos.org) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) ) #1 SMP Thu Jun 10 13:32:12 UTC 2021 JDK version: java version "1.8.0\_66" Java(TM) SE Runtime Environment (build 1.8.0\_66-b17) Java HotSpot(TM) 64-Bit Server VM (build 25.66-b17, mixed mode) elasticsearch.yml of data node cluster.name: xxx node.name: xxx thread\_pool.b…

I asked the same question on another forum, and someone replied as follows:  
In the ThreadLocal's ThreadLocalMap, there is a table. Excessive usage can result in most Entry elements in the table pointing to null, making it difficult to find a position for setting or adding objects. Additionally, the Entry objects in the table are weak references, relying on JVM GC. If using G1GC, the GC only operates on a portion of the memory area, potentially not reaching this problematic ThreadLocal for a long time, requiring a Full GC to release it immediately.

Later, I confirmed that triggering a "jmap -histo:live " to perform a Full GC can restore normal cpu behavior on the node. After changing from G1GC to CMS, the issue no longer occurred.  
Is this an Elasticsearch bug? Besides changing the GC method, are there any other ways to address this problem?

---

<div class="post-metadata">

### Author: ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)
#### Post date: [April 9, 2024, 7:03am UTC](https://discuss.elastic.co/t/how-to-avoid-incomplete-garbage-collection-by-g1gc-causing-high-cpu-usage-on-elasticsearch-nodes/357032/2 "2024-04-09T07:03:34Z")

</div>

The issue you linked to uses a very old version of Elasticsearch that has been EOL a long, long time. GIGC was never supported for this version, so if you are using that you are in unsupported territory. I would recommend you upgrade to the latest version, where GIGC is officially supported and tested.

---

<div class="post-metadata">

### Author: ![wangxr1985](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wangxr1985/32/117798_2.png) [@wangxr1985](https://discuss.elastic.co/u/wangxr1985)
#### Post date: [April 9, 2024, 7:29am UTC](https://discuss.elastic.co/t/how-to-avoid-incomplete-garbage-collection-by-g1gc-causing-high-cpu-usage-on-elasticsearch-nodes/357032/3 "2024-04-09T07:29:12Z")

</div>

This was an issue I encountered on a cluster running ES version 5.6.3, and later faced the same problem on a cluster with ES version 7.5.2. The issue was also resolved by switching from G1GC to CMS. I suspect that this problem may still exist in the latest ES version 7.

---

<div class="post-metadata">

### Author: ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)
#### Post date: [April 9, 2024, 7:32am UTC](https://discuss.elastic.co/t/how-to-avoid-incomplete-garbage-collection-by-g1gc-causing-high-cpu-usage-on-elasticsearch-nodes/357032/4 "2024-04-09T07:32:50Z")

</div>

I would recommend upgrading at least to version 7.17 and see if there are any issues when you use the default settings. Version 7.5.2 is also quite old.

---

<div class="post-metadata">

### Author: ![wangxr1985](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wangxr1985/32/117798_2.png) [@wangxr1985](https://discuss.elastic.co/u/wangxr1985)
#### Post date: [April 9, 2024, 8:02am UTC](https://discuss.elastic.co/t/how-to-avoid-incomplete-garbage-collection-by-g1gc-causing-high-cpu-usage-on-elasticsearch-nodes/357032/5 "2024-04-09T08:02:59Z")

</div>

Has anyone else reported similar issues? Can we confirm if it is a bug in ES or JDK? Would upgrading to a certain version of ES or JDK resolve it?

---

<div class="post-metadata">

### Author: ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)
#### Post date: [April 9, 2024, 8:12am UTC](https://discuss.elastic.co/t/how-to-avoid-incomplete-garbage-collection-by-g1gc-causing-high-cpu-usage-on-elasticsearch-nodes/357032/6 "2024-04-09T08:12:45Z")

</div>

Which JVM version are you using with your 7.5.2 installation? I do not remember exactly when G1GC became officially supported, but recall it required an at the time new JVM (possibly Java10 or Java11 ?) in order to be supported. G1GC was made the default later in the ES 7 series. If you upgrade to version 7.17 you should have a supported JVM bundled and G1GC set up by default if I remember correctly.

---

<div class="post-metadata">

### Author: ![wangxr1985](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wangxr1985/32/117798_2.png) [@wangxr1985](https://discuss.elastic.co/u/wangxr1985)
#### Post date: [April 9, 2024, 12:30pm UTC](https://discuss.elastic.co/t/how-to-avoid-incomplete-garbage-collection-by-g1gc-causing-high-cpu-usage-on-elasticsearch-nodes/357032/7 "2024-04-09T12:30:05Z")

</div>

use the JVM bundled:

openjdk version "13.0.1" 2019-10-15  
OpenJDK Runtime Environment AdoptOpenJDK (build 13.0.1+9)  
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 13.0.1+9, mixed mode, sharing)
