Managing Linux KVM VPS swap with vm.swappiness

Since Kernel V2.6.x users have been able to utilize VPS SWAP on Linux servers. Vm.swappiness was introduced around the time CentOS 6.x was released, many years ago now. Swap is the ability to push out completed tasks in the physical memory to the disk (swapping). Swapping usually occurs in a separate /swap partition but can also occur in a file. On the First2Host network swapping happens in a dedicated /swap partition on our PCIe NVMe sticks making it extremely fast.

More recently the idea of SWAP has been used in hardware. There is a type of hard drive you can buy called an SSHD hard drive. This uses the same idea as SWAP. It’s a standard SATA hard drive with a SWAP partition. Instead of writing data to the disk, which on a 7200RPM mechanical drive is slow, it writes the information to its SWAP or virtual memory first before transferring the data to the disk in the background making it almost as fast as an SSD drive. The downside of an SSHD drive is if you have a power cut before the data has been written to the disk then you are going to lose some information.

Managing Linux VPS SWAP with vm.swappiness

But on our KVM NVMe VPS Servers, you can manage SWAP and how your server uses SWAP with the vm.swappiness file. The default value on our network is 50 representing 50% of your memory will be free before swapping happens. The lower the value in the vm.swappiness files the less swapping that will happen. The higher the value them more swapping will happen.

0 – No swapping will happen
1 – Minimal swapping will happen but SWAP is still enabled
50 – The server will use 50/50 Swap and physical memory
100 – The server will use SWAP before physical memory

Check current vm.swappiness value

You can check your current SWAP settings by issuing the below command;

sysctl vm.swappiness

This will return the current value in the /proc/sys/vm/swappiness file. If you want to test new SWAP settings change the value in this file and monitor your SWAP usage.

Change Swappiness

To temporarily change the swappiness settings echo your new value to the

echo 50 > /proc/sys/vm/swappiness

You can also use sysctl to change the swappiness value ;

sysctl -w vm.swappiness=50

Making The Swappiness Change Permanent

This is just a temporary change. After reboot, the value will return to the previous setting. To make the change permanent you need to edit the /etc/sysctl.conf file with nano or another text editor on the command line and add the following line to the bottom of the file;

vm.swappiness = 50

Related Articles

Leave a Reply

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

Back to top button