How to change default vm.max_map_count on a Linux system

In this article we will discuss regarding vm.max_map_count and how to change this value in a linux system.

We will change the vm.max_map_count for resolution of this error returned when we try to start SonarQube installed on a linux environment with default max_map_count setting.

Max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]

As you would be aware that when we start SonarQube server, it internally initiates elasticsearch. For elasticsearch to start optimally, a few bootstrap checks are required to be passed. This is one of the bootstrap checks which elasticsearch performs.

Learn more about SonarQube:

πŸ‘‰ SonarQube integration with Jenkins

πŸ‘‰ Guide to configure a webhook in SonarQube

πŸ‘‰ To read other posts regarding SonarQube Click here

Why does elasticsearch require an increase vm. max_map_count value

This increase in virtual memory is required by elastic search as internally elasticsearch uses memory maps or mmapfs to store files for which it requires virtual memory. Elasticsearch consumes lot of memory maps, hence a lower value can cause the system to return out of memory errors when a process reaches the limit. However, we should be aware that increasing this limit will increase the memory consumption on that system as and when the extra memory is consumed for memory maps. If we want the elasticsearch to run smoothly, we need to set aside virtual memory for elasticsearch itself.

At anytime, if you want to check the current value of vm.max_map_count for your system, you can use the following command

sudo ssysctl vm.max_map_count

How to change default value for vm.max_map_count

To change the vm.max_map_count value temporarily, you can just load this value with the following command

sudo ssysctl vm.max_map_count=262144

The above command will load the max_map_count value till the next system restart.

If you are looking for a solution which will be available post restart, you will need to update the sysctl.conf file. The file sysctl.conf can be either updated in the file directly or via command line. You can choose the option as per preference and your user permissions.

How to update vm.max_map_count directly in sysctl.conf

For this you would need permission to update sysctl.conf directly. On Ubuntu server it can be found in the folder /etc/sysctl.conf. You can use any editor of your choice and open the sysctl.conf. We will use nano editor here.

sudo nano sysctl.conf

Append the following in the document. If the value for vm.max_map_count is already presebt, then you can just update as required.

vm.max_map_count=262144

This will work only when we restart the server. In order to set the value if restarting the server immediately is not an option, you can also use the following command. This command will load the values from /etc/sysctl.conf file.

sudo sysctl -p

You can also use the same command that we used earlier to load the sysctl value temporarily till we restart the system.

sudo ssysctl vm.max_map_count=262144

How to update sysctl.config via a command line option.

It is useful to first search whether the value for vm.max_map_count has already been updated in sysctl.conf file using the grep command

grep vm.max_map_count /etc/sysctl.conf

In case the value does not already exist, you can use choose one the below commands to insert the value.

Using tee command

We can use the tee command to read from the standard input and write to the standard output as well as to the sysctl.conf file as follows.

echo vm.max_map_count=262144 | sudo tee -a /etc/sysctl.conf

The tee command will write the output to the standard display and to the file specified. Do not forget the – a switch which will ensure that the file is not overwritten and the value is appended to the file.

The above command will append vm.max_map_count=262144 in the sysctl.conf file. This value will be loaded once the system reboots. If you also want to load this value before reboot, then run the below command.

echo vm.max_map_count=262144 | sudo tee -a /etc/sysctl.conf;sudo sysctl vm.max_map_count=262144

Using shell command to redirect the output

In case you want to use shell command to update the vm.max_map_count value use the following

sudo sh -c β€˜echo β€œvm.max_map_count=262144” >> /etc/sysctl.conf’;sudo sysctl vm.max_map_count=262144

This will echo the value to the standard display and redirect the same value to the sysctl.conf file. The second part of the command is to load this value temporarily as the value from sysctl.conf will be loaded only once the system restarts.

Let us know if you found this post useful.

Learn more about SonarQube:

πŸ‘‰ SonarQube integration with Jenkins

πŸ‘‰ Guide to configure a webhook in SonarQube

πŸ‘‰ To read other posts regarding SonarQube Click here

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *