As an administrator, you have face out-of-memory errors in applications. In this article, we are going to learn you how to add Swap Space on Ubuntu 20.04. Join us to review how easy you can guard against this issue after adding swap space to your server.
Table of Contents
How to add Swap Space on Ubuntu 20.04
What is Swap?
While RAM can not hold some data anymore, there is an area on the hard drive that is specified as a place where the operating system can temporarily store data. This area is called Swap. In other words, when there is no longer sufficient space in RAM to hold in-use application data, you are able to increase the amount of information that your server can keep in its working “memory”.
Although the written information to disk will be slower (than information kept in RAM), the operating system will prefer to keep running application data in memory and use swap.
1- Checking the System for Swap Information
First, check the system for any swap space available. One should be enough, though it is possible to have multiple swap files or swap partitions,
To verify this, type below command
sudo swapon --show
In case you do not receive any output, it means the system has no swap space available currently.
To check if there is any active swap or not, use free utility
free -h Output total used free shared buff/cache available Mem: 990Mi 36Mi 863Mi 4.0Mi 89Mi 837Mi Swap: 0B 0B 0B
2- Checking Available Space on the Hard Drive Partition
To check your current disk usage to make sure you have enough space, enter the following command.
df -h
Filesystem Size Used Avail Use% Mounted on udev 488M 0 488M 0% /dev tmpfs 100M 4.5M 96M 5% /run /dev/vda1 25G 989M 23G 5% / tmpfs 499M 0 499M 0% /dev/shm tmpfs 5.0M 0 5.0M 0% /run/lock tmpfs 499M 0 499M 0% /sys/fs/cgroup tmpfs 100M 0 100M 0% /run/user/1001
3- Creating a Swap File
Now you can create a swap file on your file system as you know your available hard drive space. To create a swap file use fallocate program to creates a file of the specified size. You need to create a 1G file while the server in the below example has 1G of RAM.
sudo fallocate -l 1G /swapfile
To verify the correct amount of space is reserved, enter the below command.
ls -lh /swapfile
-rw-r--r-- 1 root root 1.0G May 29 17:34 /swapfile
Buy Linux Virtual Private Server
4- Enabling the Swap File
While the file has the correct size, you should turn this into swap space. To let the only users with root privileges can read the contents, lock down the permissions of the file.
Use the following command, to make the file only accessible to root,
sudo chmod 600 /swapfil
If you want to check the permissions change, type the below command.
ls -lh /swapfile
-rw------- 1 root root 1.0G May 29 17:34 /swapfile
Mark the file as swap space by typing:
sudo mkswap /swapfile
Setting up swapspace version 1, size = 1024 MiB (1073737728 bytes) no label, UUID=b591444e-c12b-45a6-90fc-e8b24c67c006f
You can enable the swap file after marking the file to allow the system to start using it.
sudo swapon /swapfile
To check the swap is available or not, enter the following command.
sudo swapon --show
NAME TYPE SIZE USED PRIO /swapfile file 1024M 0B -2
You can check the output of the free utility again to corroborate your findings:
free -h
total used free shared buff/cache available Mem: 990Mi 37Mi 860Mi 4.0Mi 92Mi 834Mi Swap: 1.0Gi 0B 1.0Gi
5- Making the Swap File Permanent
The recent change enabled the swap file for the current session. If you reboot, the server will not retain the swap setting automatically. To change this, add the swap file to your /etc/fstab file.
If you faced anything goes wrong, back up the /etc/fstab
sudo cp /etc/fstab /etc/fstab.bak
Using the below command, add the swap file information to the end of your /etc/fstab file.
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
6- Tuning your Swap Settings
To configures how often your system swaps data out of RAM to the swap space, you can use the swappiness parameter. You can see as a value between 0 and 100 that represents a percentage. The values that are closer to 100 will try to put more data into swap in an effort to keep more RAM space free.
You can see the current swappiness value by typing:
cat /proc/sys/vm/swappiness
60
Also, you can set the swappiness to a different value by using the sysctl command.
To set the swappiness to 10, you could type:
sudo sysctl vm.swappiness=10
vm.swappiness = 10
You can set this value automatically at restart by adding the line to your /etc/sysctl.conf file
sudo nano /etc/sysctl.conf
Also, you could add:
/etc/sysctl.conf
vm.swappiness=10
Now save and close the file when you are finished.
You can also modify the vfs_cache_pressure, to configure how much the system will choose to cache inode and dentry information over other data. However, this is access data about the filesystem and is very costly to look up and very frequently requested. To check the current value by querying the proc filesystem again:
cat /proc/sys/vm/vfs_cache_pressure
100
You can set this to a more conservative setting like 50 by typing:
sudo sysctl vm.vfs_cache_pressure=50
Also, you can change that by adding it to our configuration file.
sudo nano /etc/sysctl.conf
And finally, add the line that specifies your new value:
vm.vfs_cache_pressure=50
Then save and close the file, when you are finished.
Dear user, we hope you would enjoy this tutorial on how to add Swap Space on Ubuntu 20.04 you can ask questions about this training in the comments section or to solve other problems in the field of Eldernode training, refer to the Ask page section and raise your problems in it.