This article was first published in: https://www.itcoder.tech/posts/how-to-add-swap-space-on-ubuntu-20-04/
Swap space is a space on the hard disk that is urgently requisitioned when the physical RAM memory is used up. When the RAM memory of a Linux system is exhausted, inactive memory pages will be moved to swap space.
The swap space can be an independent swap partition or a swap file. Typically, when running an Ubuntu on a virtual machine, there is no swap partition, and the only option is to create a swap file.
This tutorial explains how to add a swap file on Ubuntu 20.04.
The swap partition should not be used as a substitute for physical memory. Because the swap partition is a part of the hard drive, it has a slower access time than physical memory. If your system runs out of memory frequently, you should add more memory.
Generally, the swap file size depends on how much RAM your system has:
Only root or other users with sudo privileges can activate the swap file.
In this example, we create a 2 GB
swap file. If you want to add more swap files, replace 2G
with the size of the swap space you need to set.
Complete the following steps to add swap space on Ubuntu 20.04:
sudo fallocate -l 2G /swapfile
If the fallocate
tool is not available on your system, or you get a message: fallocate failed: Operation not supported
, use the following command to create the swap file:
sudo dd if=/dev/zero of=/swapfile bs=1024 count=2097152
600
to prevent regular users from reading and writing this file:sudo chmod 600/swapfile
sudo mkswap /swapfile
The output is as follows:
Setting up swapspace version 1, size =2GiB(2147479552 bytes)
no label, UUID=fde7d2c8-06ea-400a-9027-fd731d8ab4c8
sudo swapon /swapfile
To persist, open the /etc/fstab
file:
sudo nano /etc/fstab
And paste the following line:
/swapfile swap swap defaults 00
swapon
or free
command to verify whether the swap area is activated, like the following: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
Swappiness is a Linux kernel attribute used to define how frequently the system will use the swap partition. It ranges from 0 to 100. A lower value can avoid swap when possible, and a higher value will make the kernel use the swap partition more frequently.
On Ubuntu, the default Swappiness value is set to 60
. You can check this current value by typing the following command:
cat /proc/sys/vm/swappiness
The output is as follows:
60
Swappiness of 60 is suitable for most Linux operating systems. For production servers, you need to set this value to a lower value.
For example, to change the Swappiness value to 10
, run:
sudo sysctl vm.swappiness=10
To make this parameter persistent and still work when restarting, append the following to the /etc/sysctl.conf
file:
vm.swappiness=10
The optimal swappiness depends on your system and how the memory is used. You need to adjust this value in small steps to find the most suitable value.
To deactivate and delete the swap file, follow the steps below:
sudo swapoff -v /swapfile
Next, remove the swap file entry /swapfile swap swap defaults 0 0
from the /etc/fstab
file.
Finally, use the rm
command to delete the actual swap file:
sudo rm /swapfile
We have shown you how to create a swap file, activate, and configure swap space on your Ubuntu 20.04 system.
If you have any questions, please contact us in the following ways:
WeChat: sn0wdr1am86
WeChat group: add the above WeChat, remark the WeChat group
QQ: 3217680847
QQ Group: 82695646
Recommended Posts