Swap

Swap file configuration guide

Swap file configuration guide

Check if swap has been configured:

sudo swapon --show

If swap is configured, you’ll see something like this:

NAME      TYPE SIZE  USED PRIO
/swapfile file   2G 10.3M   -2

If the system does not have swap space available currently, you’ll get no output.

The free utility can also be used to verify that there is no active swap:

free -h

Expected output:

              total        used        free      shared  buff/cache   available
Mem:          981Mi       122Mi       647Mi       0.0Ki       211Mi       714Mi
Swap:            0B          0B          0B

Check available disk space

df -h

Create a swap file

To allocate 1G swap file:

sudo fallocate -l 1G /swapfile

To verify:

ls -lh /swapfile

Expected output:

-rw-r--r-- 1 root root 1.0G Apr 25 11:14 /swapfile

Enable the swap file

Make the file only accessible to root:

sudo chmod 600 /swapfile

Verify the permissions:

ls -lh /swapfile

Expected output:

-rw------- 1 root root 1.0G Apr 25 11:14 /swapfile

Make the file as swap space:

sudo mkswap /swapfile

Enable the swap file:

sudo swapon /swapfile

Verify the swap is available:

sudo swapon --show

Expected output:

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

You can also verify using the free utility:

free -h

Expected output:

              total        used        free      shared  buff/cache   available
Mem:          981Mi       123Mi       644Mi       0.0Ki       213Mi       714Mi
Swap:         1.0Gi          0B       1.0Gi

Make the swap file permanent

This file will only be enabled for the current session. The setting will not be retained after reboot. To make it permanent, you need to configure the /etc/fstab file.

Start by making a backup of /etc/fstab:

sudo cp /etc/fstab /etc/fstab.bak

Open /etc/fstab in nano:

sudo nano /etc/fstab

Add the following config at the end of the file:

/swapfile none swap sw 0 0

/swapfile: This is the path to the swap file.

none: This field is for specifying a device or filesystem type, but since it’s a swap file, it’s set to none.

swap: This specifies that the filesystem type is swap space.

sw: This option tells the system to use this entry as a swap space.

0: This value is used by the dump utility to determine if the filesystem should be backed up. 0 means it will not be backed up.

0: This value is used by fsck (filesystem check) to determine the order in which filesystems should be checked at boot time. 0 means it will not be checked.

Now the swap space is good to go!

Tuning swap settings

Swappiness

The swappiness variable controls how the system switches between the main memory (RAM) and swap memory on your server based on the usage percentage. A low value minimizes swapping to disk while a higher value enables the server to use swap memory based on the following ratings:

0: Enables the system to avoid swapping processes out of physical memory.

1-49: Enables swapping with reduced usage unless the main memory (RAM) is at capacity.

50: Balances swapping and memory caching on the server.

51-99: Increases swapping from the main memory to swap memory.

100: Prioritizes swapping on the server and processes are continuously moved from RAM to swap.

Cache Pressure

The vfs_cache_pressure variable controls how much the system will choose to cache inode and dentry information over pagecache and swap. Increasing this value increases the rate at which VFS caches are reclaimed.

Basically, this is access data about the filesystem. This is generally very costly to look up and very frequently requested, so it’s an excellent thing for your system to cache.

Tuning

To see the current swappiness and vfs_cache_pressure values:

cat /proc/sys/vm/swappiness
cat /proc/sys/vm/vfs_cache_pressure

To change the swappiness and vfs_cache_pressure values:

sudo nano /etc/sysctl.conf

Add the following config at the end of the file, adjust the exact values to your likings:

vm.swappiness=10
vm.vfs_cache_pressure=50

Apply by running:

sudo sysctl -p

References

https://www.digitalocean.com/community/tutorials/how-to-add-swap-space-on-ubuntu-22-04

https://docs.vultr.com/how-to-add-swap-memory-in-ubuntu-24-04

https://gist.github.com/marianposaceanu/5629740

Content Licensed under CC BY-SA 4.0. Code licensed under the MIT License.