When you run out of physical RAM on a Linux server, the kernel moves inactive memory pages to a dedicated space on your hard drive or SSD known as “swap space.” This prevents the system from crashing due to Out Of Memory (OOM) errors, but it comes at a significant performance cost, as reading and writing to a disk is exponentially slower than accessing RAM. The Linux kernel uses a specific configuration parameter called swappiness to determine exactly how aggressively it should utilise this swap space. By understanding and tuning the vm.swappiness value, you can force your server to rely more heavily on physical RAM, dramatically improving database responsiveness and overall system performance.
Understanding the Swappiness Value
The swappiness parameter is a numerical value ranging from 0 to 100.
- A value of 0 instructs the kernel to avoid swapping processes out of physical memory for as long as absolutely possible. It will only use swap space if the physical RAM is completely exhausted and an OOM condition is imminent.
- A value of 100 tells the kernel to aggressively swap inactive memory pages to disk almost immediately, freeing up physical RAM for other uses, such as file system caching.
- The default value on most modern Linux distributions, including Ubuntu and Debian, is 60.
While a default of 60 is a reasonable compromise for desktop environments where you might leave dozens of idle web browser tabs open, it is generally inappropriate for database servers (like MySQL, PostgreSQL, or Redis) which require their working datasets to remain in physical RAM for high-speed access.
Checking the Current Swappiness Value
Before making any changes, you should verify the current configuration on your server.
- Log into your Linux server via SSH.
- Run the following command to print the current value directly from the kernel configuration file:
cat /proc/sys/vm/swappiness - The output will simply be the number, usually 60.
Testing a New Swappiness Value Temporarily
You can change the swappiness value on the fly without rebooting the server. This allows you to test the performance impact of a new setting immediately. For most web servers and database hosts with adequate RAM, a value of 10 is highly recommended.
- Use the
sysctlcommand to modify the running kernel parameter:sudo sysctl vm.swappiness=10 - Verify the change was successful by running:
cat /proc/sys/vm/swappiness
This change takes effect immediately. The kernel will now heavily resist writing to the swap file unless absolutely necessary. However, because this change was made in memory, it will not survive a system reboot.
Making the Swappiness Change Permanent
Once you have confirmed that the new value of 10 improves performance and does not cause OOM errors during peak traffic, you must write the change to the system configuration files so it is applied automatically every time the server starts.
- Open the sysctl configuration file using the nano text editor:
sudo nano /etc/sysctl.conf - Scroll to the very bottom of the file.
- Add the following line on a new row:
vm.swappiness=10 - Save the file and exit the editor (in nano, press Ctrl+O, press Enter, then press Ctrl+X).
When Should You Use a Higher Value?
Lowering the swappiness value to 10 is considered a best practice for production servers running dedicated applications like web servers or databases. However, there are scenarios where higher values are appropriate. If you are running a server with extremely limited physical RAM (such as a cheap 512MB VPS) and you are running multiple idle background services, keeping the default value of 60 ensures those idle services are pushed to swap, leaving the small amount of physical RAM available for the web server to handle active incoming requests.