Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change Elasticsearch max memory size

People also ask

How do I reduce Elasticsearch RAM usage?

You can avoid memory usage issues in Elasticsearch by running the Elasticsearch Health Check-Up. It will help check for many configuration errors that are related to memory usage in your cluster and offer overall improvements and suggestions to your setup.

How much memory should I allocate to Elasticsearch?

As a Java application, Elasticsearch requires some logical memory (heap) allocation from the system's physical memory. This should be up to half of the physical RAM, capping at 32GB.

What is the XMS and XMX values recommended for Elasticsearch?

Set Xms and Xmx to no more than 50% of your total memory. Elasticsearch requires memory for purposes other than the JVM heap.


In ElasticSearch >= 5 the documentation has changed, which means none of the above answers worked for me.

I tried changing ES_HEAP_SIZE in /etc/default/elasticsearch and in /etc/init.d/elasticsearch, but when I ran ps aux | grep elasticsearch the output still showed:

/usr/bin/java -Xms2g -Xmx2g # aka 2G min and max ram

I had to make these changes in:

/etc/elasticsearch/jvm.options

# Xms represents the initial size of total heap space
# Xmx represents the maximum size of total heap space

-Xms1g 
-Xmx1g 
# the settings shipped with ES 5 were: -Xms2g
# the settings shipped with ES 5 were: -Xmx2g

Updated on Nov 24, 2016: Elasticsearch 5 apparently has changed the way to configure the JVM. See this answer here. The answer below still applies to versions < 5.

tirdadc, thank you for pointing this out in your comment below.


I have a pastebin page that I share with others when wondering about memory and ES. It's worked OK for me: http://pastebin.com/mNUGQCLY. I'll paste the contents here as well:

References:

https://github.com/grigorescu/Brownian/wiki/ElasticSearch-Configuration http://www.elasticsearch.org/guide/reference/setup/installation/

Edit the following files to modify memory and file number limits. These instructions assume Ubuntu 10.04, may work on later versions and other distributions/OSes. (Edit: This works for Ubuntu 14.04 as well.)

/etc/security/limits.conf:

elasticsearch - nofile 65535
elasticsearch - memlock unlimited

/etc/default/elasticsearch (on CentOS/RH: /etc/sysconfig/elasticsearch ):

ES_HEAP_SIZE=512m
MAX_OPEN_FILES=65535
MAX_LOCKED_MEMORY=unlimited

/etc/elasticsearch/elasticsearch.yml:

bootstrap.mlockall: true

For anyone looking to do this on Centos 7 or with another system running SystemD, you change it in

/etc/sysconfig/elasticsearch 

Uncomment the ES_HEAP_SIZE line, and set a value, eg:

# Heap Size (defaults to 256m min, 1g max)
ES_HEAP_SIZE=16g

(Ignore the comment about 1g max - that's the default)


Instructions for ubuntu 14.04:

sudo vim /etc/init.d/elasticsearch

Set

ES_HEAP_SIZE=512m

then in:

sudo vim /etc/elasticsearch/elasticsearch.yml

Set:

bootstrap.memory_lock: true

There are comments in the files for more info


Previous answers were insufficient in my case, probably because I'm on Debian 8, while they were referred to some previous distribution.

On Debian 8 modify the service script normally place in /usr/lib/systemd/system/elasticsearch.service, and add Environment=ES_HEAP_SIZE=8G just below the other "Environment=*" lines.

Now reload the service script with systemctl daemon-reload and restart the service. The job should be done!


Create a new file with the extension .options inside /etc/elasticsearch/jvm.options.d and put the options there. For example:

sudo nano /etc/elasticsearch/jvm.options.d/custom.options

and put the content there:

# JVM Heap Size - see /etc/elasticsearch/jvm.options
-Xms2g
-Xmx2g

It will set the maximum heap size to 2GB. Don't forget to restart elasticsearch:

sudo systemctl restart elasticsearch

Now you can check the logs:

sudo cat /var/log/elasticsearch/elasticsearch.log | grep "heap size"

You'll see something like so:

heap size [2gb], compressed ordinary object pointers [true]

Doc


If you use the service wrapper provided in Elasticsearch's Github repository, found at https://github.com/elasticsearch/elasticsearch-servicewrapper, then the conf file at elasticsearch-servicewrapper / service / elasticsearch.conf controls memory settings. At the top of elasticsearch.conf is a parameter:

set.default.ES_HEAP_SIZE=1024

Just reduce this parameter, say to "set.default.ES_HEAP_SIZE=512", to reduce Elasticsearch's allotted memory.

Note that if you use the elasticsearch-wrapper, the ES_HEAP_SIZE provided in elasticsearch.conf OVERRIDES ALL OTHER SETTINGS. This took me a bit to figure out, since from the documentation, it seemed that heap memory could be set from elasticsearch.yml.

If your service wrapper settings are set somewhere else, such as at /etc/default/elasticsearch as in James's example, then set the ES_HEAP_SIZE there.