How to add swap space to your Ubuntu
Swap space is the area on a hard disk. It holds memory pages that are temporarily inactive. Swap space is used when your operating system decides that it needs physical memory for active processes and the amount of unused physical memory is insufficient. When this happens, inactive pages from the physical memory are then moved into the swap space, freeing up that physical memory for other uses.
How much swap do I need?
For modern systems, your swap space should be at a minimum be equal to your physical memory (RAM) size if you use hibernation, otherwise you need a minimum of round(sqrt(RAM)) and a maximum of twice the amount of RAM depending upon the amount of hard disk space available for the system.
How do I add swap partition?
Dedicated Swap partition is the recommended option. However, it can be risky to assign or change partitions when you already have an operating system installed. In this case, we recommend to use the Swap file instead.
If you are installing a fresh operating system you can follow installation guide steps to setup Swap partition.
How do I add swap file?
We will allocate a file of a 4GB file called swapfile in our root (/) directory:
sudo fallocate -l 4g /swapfile
If fallocate fails or it's not available, you can use dd (4G = 4194304B):
sudo dd if=/dev/zero of=/swapfile bs=1024 count=4194304
We need to set the swap file permissions to 600 to prevent other users from being able to read potentially sensitive information from the swap file:
sudo chmod 600 /swapfile
Format the file as swap and enable it:
sudo mkswap /swapfile && sudo swapon /swapfile
To make the change permanent open the /etc/fstab file:
sudo nano /etc/fstab
and paste the following line:
/swapfile swap swap defaults 0 0
Finally, verify that the swap is active:
sudo swapon --show
Adjusting the swappiness
Swappiness is a Linux kernel property that defines how often the system will use the swap space. It can have a value between 0 and 100. A low value will make the kernel to try to avoid swapping whenever possible, while a higher value will make the kernel to use the swap space more aggressively.
You can check the current value by typing the following command:
While the swappiness value of 60 is OK for most Linux systems, for production servers, you may need to set a lower value.
To make this parameter persistent across reboots, open /etc/sysctl.conf file:
sudo nano /etc/sysctl.conf
The optimal swappiness value depends on your system workload and how the memory is being used. You should adjust this parameter in small increments to find an optimal value.
Disable and remove a Swap file
Disable the swap file from the running system:
sudo swapoff /swapfile
Next, remove the swap file entry from the /etc/fstab file:
/swapfile swap swap defaults 0 0
Finally, remove the actual swapfile file using the rm command:
sudo rm /swapfile
You can use the following command to disable swap and get it manually back into RAM:
sudo swapoff -a
And then, enable it again:
sudo swapon -a
Don't forget that the access time for swap is slower, depending on the speed of the hard drive. Do not consider it to be a complete replacement for the physical memory. Swap space can be a dedicated swap partition (recommended), a swap file, or a combination of swap partitions and swap file(s).