Sun

SWAP memory allocation

  1. Knowledge Base
  2. Hosting
  3. VPS Hosting
  4. SWAP memory allocation

SWAP memory is used to help RAM, once it can not store anymore data. The data which can not be stored in RAM, is then stored to SWAP memory in the hard disk. In this article we will provide the basic steps how to modify and increase SWAP memory in our KVM VPS.

In general, there are different opinions on how much SWAP memory your server should have. It could be half of your RAM, the same amount or even more than RAM. In our article we will assign 6 GB of SWAP to the server.

Allocating SWAP memory

First thing you should do is to check if there is not SWAP memory in use on your server with the following command:

free -h

your results will be printed in two lines: “Mem”, “Swap”, which will indicates what is the exact amount of RAM and SWAP memory on server. The “Swap” line should only contain zeros.

With the following command we will allocate 6 GB of disk space for our SWAP memory:

fallocate -l 4G /swapfile

You can check if your SWAP memory was assigned with this command:

ls -lh /swapfile

At first, your SWAP may not be allocated due to permission issue and you might be seeing this message:

-rw-r--r-- 1 root root 6.0G Dec 5 14:32 /swapfile

This would suggest we have to make additional changes, first of which should be changing the permission of the swapfile:

chmod 600 /swapfile

After the change you can check the file permissions again:

ls -lh /swapfile

The results should change as well, comparing to the previous above:

-rw------- 1 root root 6.0G Dec  5 14:36 /swapfile

We can now check if the SWAP memory was allocated correctly:

free -h

Your results will print two lines again, just this time, you will see a line “Swap” having a variable of 6 GB.

Additional options

In general your SWAP memory allocation may stop working after you reboot the server, so in order to save these changes permanently, we have to edit /etc/fstab. But first we would recommend making a backup of this file:

cp /etc/fstab /etc/fstab.old

Once the backup is done, we have make the changes to the actual file:

echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

There are few extra option for SWAP memory, which we would like to address as well.

option – swappiness, is used to describe when the system will move data to SWAP file. The option may have a value from 0 to 100. Closer to 0, means that your data will be move to SWAP only when it will be necessary.  Closer to 100 means that, data will be moved to SWAP more often, therefore leaving RAM memory more free. We would recommend keeping this option closer to 0, for example 10.

You can check the current value with the following command:

cat /proc/sys/vm/swappiness

option – vfs_cache_pressure, this option sets how often the information about file system is updated. By default it should be 100, but we would recommend using low, for example, value 50.

The current value can be checked with this command:

cat /proc/sys/vm/vfs_cache_pressure

Both of the additional options can be edited at the file /etc/sysctl.conf by adding the following line at the bottom of the file:

vm.swappiness=10
vm.vfs_cache_pressure=50

Was this article helpful?

Related Articles