Information

Swap space is a space on a hard disk that is a substitute for physical memory. If the system needs more memory resources and the RAM is full, inactive pages in memory are moved to the swap space.

While swap space can help machines with a small amount of RAM, it should not be considered a replacement for more RAM. Swap space is located on hard drives, which inherently have a slower access time than physical memory.

⚠️ LXC Containers & OpenVZ ⚠️

Creating Swap Space in LXC Containers & OpenVZ is not supported.

Step 1 – Check the System for Swap Information

Firstly, check if the system has any swap configured

sudo swapon --show

If you receive no output it means your system does not have swap space setup.

Check there is not swap active using the free command:

free -h
              total        used        free      shared  buff/cache   available
Mem:          2.4Gi       887Mi       462Mi       2.0Mi       1.1Gi       1.4Gi
Swap:            0B          0B          0B

Step 2 – Checking Available Hard Drive Space

Before creating a swap file, make sure we’ll make sure we have enough space on the HDD.

df -h

Our output should look something like this:

Filesystem      Size  Used Avail Use% Mounted on
udev            1.2G     0  1.2G   0% /dev
tmpfs           249M  1.1M  248M   1% /run
/dev/vda2        44G  4.4G   38G  11% /
tmpfs           1.3G     0  1.3G   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           1.3G     0  1.3G   0% /sys/fs/cgroup
/dev/loop1       50M   50M     0 100% /snap/snapd/17950

/dev/vda2 has 38GB available on this system so we’re good to go.

Step 3 – Creating the Swap File

Rule of thumb is that anything over 4GB of swap is probably unnecessary if you are just using it as RAM fallback. Generally, an amount equal to or double the amount of RAM on your system is a good starting point, however it really depends on your personal preferences and your application requirements.

For this guide, we’re only going to create a 1GB swap file.

fallocate -l 1G /swapfile

Check that the correct amount of space was reserved.

ls -lh /swapfile

Our output should look like this:

-rw-r--r-- 1 root root 1.0G Jan 21 04:29 /swapfile

Step 4 – Enabling the Swap File

Start by making the file accessible to root using chmod:

chmod 600 /swapfile

Check the permissions:

ls -lh /swapfile

Our output should look like this:

-rw------- 1 root root 1.0G Jan 21 04:29 /swapfile

This means that the root user has read write access to the swap file.

Mark the file as swap space:

mkswap /swapfile

Our output should look like this:

Setting up swapspace version 1, size = 1024 MiB (1073737728 bytes)
no label, UUID=0aefef0e-4ca8-4894-8d46-8c0d6cee0ef1

After marking the file you can enable the swap space:

swapon /swapfile

Verify the enabling the swap space worked:

sudo swapon --show

Our output should look like this:

NAME      TYPE  SIZE USED PRIO
/swapfile file 1024M   0B   -2

Use the free command again to make sure the swap has applied correctly.

free -h

Step 5 – Make the Swap Space Persist

Currently the swap space is active & availble, but if we rebooted the server would not retain the changed, we can make this permanent by adding the swap space to our /etc/fstab file. Back up the /etc/fstab file in case anything goes wrong:

cp /etc/fstab /etc/fstab.bak

Add the swap space info to your /etc/fstab file by typing:

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

Step 6 – Tuning your Swappiness

swappiness configures how often your system swaps data out of RAM, this is set between 0 & 100 which represents a percentage.

Check the current swappiness value:

cat /proc/sys/vm/swappiness

Our output should look like this:

60

For this guide we’ll set the swappiness to 10:

sysctl vm.swappiness=10

Our output should look like this:

vm.swappiness = 10

This will persist until the server reboots so we’ll add it to our /etc/sysctl.conf file:

nano /etc/sysctl.conf

At the bottom add:

vm.swappiness=10

Final Notes & Further Reading

Congratulations on setting up swap space on your server.

You can also adjust the cache_pressure setting, this configures how much the system will choose to cache inode and dentry information, it’s pretty much data about the filesystem and can take a toll on the system to look up, so it tends to be a great thing for your system to cache - there is a great guide available for that at linuxadictos.com