SWAP memory allocation

SWAP memory allocation

Summary:

SWAP memory is used to help RAM, once it can not store any more data. Here is how to modify and increase SWAP memory on Linux.

SWAP memory is used to help RAM, once it can not store any more data. The data which can not be stored in RAM is then stored to SWAP memory in the hard disk. Here is how to modify and increase SWAP memory on Linux.

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 this case, I 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 indicate what is the exact amount of RAM and SWAP memory on the server. If SWAP is not yet configured the “Swap” line should only contain zeros.

testsrv - SWAP memory allocation

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

fallocate -l 1G /swapfile

fallocate -change the allocated disk space for a file, either to deallocate or preallocate it ( e.g increase it by 1GB from 11G to 12G).

Suppose fallocate fails or is not available, you can also use the dd command:

dd if=/dev/zero of=/swapfile bs=1024 count=1048576

dd command - SWAP memory allocation

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 a permission issue and you might be seeing this message:

-rw-r--r-- 1 root root 12.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, compared to the previous above:

-rw------- 1 root root 12.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 12 GB.

12gb swap file - SWAP memory allocation

Make SWAP file permanent

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 can make the changes to the actual file:

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

Additional Swap options

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 lines at the bottom of the file:

vm.swappiness=10
vm.vfs_cache_pressure=50
whoami
Stefan Pejcic
Join the discussion

I enjoy constructive responses and professional comments to my posts, and invite anyone to comment or link to my site.