How To Add Swap Space on Ubuntu 20.04
21/01/2023
LXC Containers & OpenVZ
Creating Swap Space in LXC Containers & OpenVZ is not supported.
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.
Step 1 - Check the System for Swap Information
Firstly, check if the system has any swap configured
sudo swapon --show
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
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 a 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.
sudo fallocate -l 1G /swapfile
ls -lh /swapfile
-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:
sudo chmod 600 /swapfile
ls -lh /swapfile
-rw------- 1 root root 1.0G Jan 21 04:29 /swapfile
Mark the file as swap space:
sudo mkswap /swapfile
Setting up swapspace version 1, size = 1024 MiB (1073737728 bytes)
no label, UUID=0aefef0e-4ca8-4894-8d46-8c0d6cee0ef1
sudo swapon /swapfile
sudo swapon --show
NAME TYPE SIZE USED PRIO
/swapfile file 1024M 0B -2
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:
sudo cp /etc/fstab /etc/fstab.bak
echo '/swapfile none swap sw 0 0' | sudo 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
60
sudo sysctl vm.swappiness=10
vm.swappiness = 10
/etc/sysctl.conf file: sudo nano /etc/sysctl.conf
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 & 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