Elasticsearch Elasticsearch Java Heap Size

By Opster Team

Updated: Jun 22, 2023

| 2 min read

Optimizing Elasticsearch Java Heap Size

When running Elasticsearch, one of the critical factors that can impact performance and stability is the Java Heap Size. This article will guide you through the process of optimizing the Java Heap Size for your Elasticsearch cluster to ensure efficient resource utilization and prevent OutOfMemoryError issues.

If you want to learn about Elasticsearch heap size usage & JVM garbage collection, check out this guide.

Optimizing the Java Heap Size for your Elasticsearch cluster

1. Determine the appropriate Java Heap Size

The first step in optimizing the Java Heap Size is determining the appropriate value for your environment. The general recommendation is to allocate 50% of the available system memory to Elasticsearch, with a maximum of 32GB. This is because Elasticsearch can efficiently utilize compressed object pointers (CompressedOops) when the heap size is below 32GB.

For example, if your system has 64GB of memory, you should allocate 32GB to Elasticsearch:

-Xms32g -Xmx32g

2. Configure Elasticsearch JVM options

To set the Java Heap Size, you need to create  a custom Elasticsearch JVM options file with the `.options` extension and store it in the Elasticsearch `config/jvm.options.d/` folder. In that file, you can set the `-Xms` and `-Xmx` values to match your desired heap size. For example:

-Xms32g
-Xmx32g

3. Monitor and adjust the Java Heap Size

After configuring the Java Heap Size, it’s essential to monitor the Elasticsearch cluster’s performance and resource utilization. Use the Cluster Stats API to gather information about the heap usage:

GET /_cluster/stats

Pay attention to the `jvm.mem.heap_used_in_bytes` and `jvm.mem.heap_max_in_bytes` values. If the heap usage is consistently above 75%, consider increasing the heap size. Conversely, if the heap usage is consistently below 50%, you may be able to reduce the heap size to free up system resources for other processes or reduce the host physical memory to lower the costs

4. Consider Garbage Collection (GC) tuning

In some cases, adjusting the Java Heap Size alone may not be sufficient to optimize Elasticsearch performance. You may also need to tune the Garbage Collection settings to minimize GC pauses and reduce the impact on query and indexing performance. Some commonly adjusted GC settings include:

– `XX:MaxGCPauseMillis`: Sets a target for the maximum GC pause time.
– `XX:G1NewSizePercent`: Adjusts the percentage of the heap allocated to the young generation.
– `XX:G1ReservePercent`: Sets the percentage of the heap reserved as a buffer for GC.

For example, to set a maximum GC pause time of 200 milliseconds, add the following line to your custom JVM options file:

-XX:MaxGCPauseMillis=200

Conclusion

In conclusion, optimizing the Java Heap Size and Garbage Collection settings can significantly improve the performance and stability of your Elasticsearch cluster. Regularly monitor your cluster’s resource utilization and adjust the settings as needed to ensure efficient operation.