Home‎ > ‎Server config‎ > ‎

RHEL/CentOS memory overcommit kernel settings

Disable over commit

In major Linux distributions, the kernel allows by default for processes to request more memory than is currently free in the system to improve the memory utilization. This is based on heuristics that the processes never truly use all the memory they request. However, if your system is at risk of running out of memory, and you wish to prevent losing tasks to OOM killer, it is possible to disallow memory overcommit.

To change how the system handles overcommit calls Linux has an application called ‘sysctl’ that is used to modify kernel parameters at runtime. You can list all sysctl controlled parameters using the following.

sysctl -a

The particular parameters that control memory are very imaginatively named vm.overcommit_memory and vm.overcommit_ratio. To change the overcommit mode, use the below command.

sysctl -w vm.overcommit_memory=2

This parameter has 3 different values:

  • 0 means to “Estimate if we have enough RAM”
  • 1 equals to “Always allow”
  • 2 that is used here tells the kernel to “Say no if the system doesn’t have the memory”

The important part of changing the overcommit mode is to remember to also change the overcommit_ratio. When overcommit_memory is set to 2, the committed address space is not permitted to exceed swap space plus this percentage of physical RAM. To be able to use all of the system’s memory use 100 in the next command. I'm using 95% as a safeguard for the kernel.

sysctl -w vm.overcommit_ratio=95

These changes are applied immediately but will only persist until the next system reboot. To have the changes remain permanent, the same parameter values need to be added to sysctl.conf –file. Open the configuration file for edit.

nano /etc/sysctl.conf

Add the same lines to the end of the file.

vm.overcommit_memory=2vm.overcommit_ratio=95

Save the changes (ctrl + O) and exit (ctrl + X) the editor. Your server will read the configurations every time at boot up, and prevent applications from overcommitting memory.