Swap is space on a hard drive that is used when the physical RAM memory is full. When a Linux system runs out of memory, inactive pages are moved from memory to the swap area.
The swap area can either take the form of a dedicated swap partition or a swap file. When Ubuntu is running on a virtual machine, there is usually no swap partition and the only option is to create a swap file.
This tutorial explains how to add a paging file on Ubuntu 20.04.
before you start
Swap should not be viewed as a substitute for physical storage. Since the swap memory is a section of the hard disk, it has a slower access time than the physical memory. If your system is constantly running out of memory, consider adding more RAM.
In general, the size of the paging file depends on how much RAM your system has:
- Systems with less than 2 GB of RAM – 2 times the amount of RAM.
- Systems with 2 to 8 GB of RAM – the same size as the amount of RAM.
- Systems with more than 8 GB of RAM – at least 4 GB swap.
Only root or users with sudo permissions can activate the swap file.
Create a swap file
In this example we are creating
2 GB Paging file. If you want to add more swap replace it
2G with the amount of swap space you need.
To add swap space on Ubuntu 20.04, do the following:
First, create a file that will be used as a swap:
sudo fallocate -l 2G /swapfile
fallocateUtility does not exist on your system or you receive an error message stating
fallocate failed: Operation not supported, use the following command to create the paging file:
sudo dd if=/dev/zero of=/swapfile bs=1024 count=2097152
Set the file permissions
600to prevent normal users from writing and reading the file:
sudo chmod 600 /swapfile
Create a Linux swap area for the file:
sudo mkswap /swapfile
Setting up swapspace version 1, size = 2 GiB (2147479552 bytes) no label, UUID=fde7d2c8-06ea-400a-9027-fd731d8ab4c8
Activate the paging file by running the following command:
sudo swapon /swapfile
To make the change permanent, open that
sudo nano /etc/fstab
and insert the following line:/ etc / fstab
/swapfile swap swap defaults 0 0
Make sure the swap is active by using either the
Command as shown below:
sudo swapon --show
NAME TYPE SIZE USED PRIO /swapfile file 2G 0B -1
sudo free -h
total used free shared buff/cache available Mem: 981Mi 97Mi 68Mi 0.0Ki 814Mi 735Mi Swap: 2.0Gi 10Mi 1.9Gi
Adjusting the swap value
Swappiness is a property of the Linux kernel that defines how often the system uses the swap area. It can have a value between 0 and 100. A lower value will cause the kernel to try to avoid swapping whenever possible, while a higher value will cause the kernel to use swap space more aggressively.
Under Ubuntu, the default value for swappiness is set to
60. You can check the current value by entering the following command:
While the swappiness value of
60 is fine for most Linux systems, but may need to be set lower for production servers.
To set the swappiness value, for example, to
sudo sysctl vm.swappiness=10
To make this parameter persistent across reboots, add the following line to the
The optimal swappiness value depends on your system utilization and the use of memory. You should adjust this parameter in small steps to find an optimal value.
Remove a paging file
Proceed as follows to deactivate and delete the paging file:
First deactivate the swap space:
sudo swapoff -v /swapfile
Next, remove the paging file entry
/swapfile swap swap defaults 0 0of the
Finally, remove the actual paging file with the
sudo rm /swapfile
We showed you how to create a swap file and activate and configure the swap area on your Ubuntu 20.04 system.
If you run into any problem or have any feedback, please leave a comment below.