Using swap within a VPS

In a virtual environment, disk io can be about the most expensive operation there is. And swap (virtual memory) depends on that heavily. By default we configure a small swap partition for each VPS server so conditions where memory is exhausted can be detected more consistently, but that should not be used as a RAM substitute.

Swapfiles (manually created file based swap) are not recommended on our VPS servers since they often degrade the performance of your service when actively used.

Our hosts are all monitored in case load on different subsystems are causing issues. And we might send you an alert if your swap space is being used a lot since that often indicates problems with the VPS. In some cases we might limit that usage to prevent other customers on the same host from being affected.

If your server is consistently using swap space, and you have tuned resource usage by your services as much as possible, we highly recommend adding more RAM. You can do that at any time via our handy resource change tool

To check if your server is swapping you can use the free command. In the following example you can see that a small amount of swap space has been used on the third line, which is ok in this case.

~$ free -m
             total       used       free     shared    buffers     cached
Mem:          3962       3694        267          0        297       1442
-/+ buffers/cache:       1953       2008
Swap:          128          4        124

One way to help the VPS be less likely to prefer swap space is to tweak the swappiness kernel parameter. As the root user you can temporarily set that to a value between 0 and 100 with the following command (lower values means less likely to swap, default is 60):

echo 30 > /proc/sys/vm/swappiness

You can make that permanent by adding the parameter to the end of the /etc/sysctl.conf like the following:

vm.swappiness = 30

Any changes to the sysctl.conf file can be loaded by rebooting your VPS, or more nicely with:

sysctl -f /etc/sysctl.conf

If you decide you want more swap space you can add a swap file using something like:

dd if=/dev/zero of=$swapfile bs=1024 count=$[$size*1024]

mkswap $swapfile
swapon -p -2 $swapfile
echo "$swapfile swap swap defaults,noatime,pri=-2 0 0" >> /etc/fstab
mount -a
swapon -a
swapon -s
#NB: change /etc/fstab for main swap to make it pri=3 (mean it'll be used first
This entry was posted in Rimuhosting and tagged , , , , , , . Bookmark the permalink.

Comments are closed.