0
0
ElasticsearchDebug / FixIntermediate · 4 min read

How to Fix Out of Memory Errors in Elasticsearch

To fix OutOfMemoryError in Elasticsearch, increase the JVM heap size by setting ES_JAVA_OPTS or editing jvm.options. Also, optimize queries and reduce heavy aggregations to lower memory use.
🔍

Why This Happens

Elasticsearch runs on Java and uses a heap to manage memory. If the heap is too small or queries are too heavy, Elasticsearch runs out of memory and crashes. This usually happens when large aggregations or many shards consume more memory than allocated.

bash
curl -X GET "localhost:9200/_search" -H 'Content-Type: application/json' -d'{"aggs": {"big_agg": {"terms": {"field": "large_field", "size": 100000}}}}'
Output
java.lang.OutOfMemoryError: Java heap space
🔧

The Fix

Increase the JVM heap size to give Elasticsearch more memory. This is done by setting ES_JAVA_OPTS environment variable or editing the jvm.options file. Also, optimize queries by reducing aggregation sizes or using filters.

bash
# Set heap size to 4GB
export ES_JAVA_OPTS="-Xms4g -Xmx4g"

# Example optimized query with smaller aggregation size
curl -X GET "localhost:9200/_search" -H 'Content-Type: application/json' -d'{"aggs": {"small_agg": {"terms": {"field": "large_field", "size": 1000}}}}'
Output
{"took": 10, "timed_out": false, "_shards": {"total": 5, "successful": 5}, "aggregations": {"small_agg": {"buckets": [...]}}}
🛡️

Prevention

To avoid out of memory errors, always set the heap size to about half of your server's RAM but no more than 32GB. Monitor memory usage regularly and optimize queries to avoid large aggregations or deep pagination. Use index lifecycle management to reduce shard count and size.

⚠️

Related Errors

Other memory-related errors include GC overhead limit exceeded which means garbage collection is taking too long, and too many open files which can be fixed by increasing OS limits. Both require resource tuning and monitoring.

Key Takeaways

Increase Elasticsearch JVM heap size to fix out of memory errors.
Optimize queries to reduce memory consumption, especially large aggregations.
Set heap size to half of server RAM but not more than 32GB for best performance.
Monitor Elasticsearch memory usage and shard sizes regularly.
Use index lifecycle management to control shard count and size.