How To Add Swap Space on Ubuntu

Add Swap Space on Ubuntu

How to add swap space on Ubuntu 20.04

Ubuntu How to Add Swap Space: This step-by-step tutorial will show you how to create swap space on Ubuntu. By boosting your virtual memory right now, you may improve the efficiency of your system and prevent crashes. Simple to follow directions for both novice and experienced users.

When the physical RAM memory is eaten up, swap space on the disc is utilized. Inactive pages are relocated to the swap space when a Linux system runs out of Memory.

What exactly is Swap?

Swap is a section of hard disc storage reserved for the operating system to temporarily store data that it cannot keep in RAM. With some limitations, this allows you to expand the amount of information that your server can hold in its working memory. The swap space on the hard disc will be used mostly when RAM no longer has enough space to keep in-use program data.

Before we begin, we may see if the system has any accessible swap space. It is possible to have numerous swap files or swap partitions, but in most cases, one should be enough.

sudo swapon --show

#
# If you receive no response, this indicates that your system currently lacks swap space.
#

A dedicated swap partition or a swap file can both be used as swap space. A swap partition is typically absent when running Ubuntu on a virtual machine, leaving the user with the option of creating a swap file instead.

How to add a swap file on Ubuntu 20.04 is described in this guide.

Let’s start

Swap should not be regarded as a substitute for physical memory. Swap space has a slower access time than physical memory because it is a section of the hard drive. If your system is constantly out of memory, you should upgrade your RAM.

The swap file can only be activated by root or a user with sudo privileges.

How much swap do I require?

Swap RAM depends on how much RAM your system already has.

Sl. No.Available RAM SpaceRecommended Swap Space
11GB1GB
22GB1GB or 1500MB
33GB2GB or 2GB
44GB2GB
55GB2GB
68GB3GB
How much swap do I require?

First, create a swap file

In this example, we will create a swap file of 2 GB. If you need more swap space, replace 2G with the size you require.

To add swap space on Ubuntu 20.04, follow the steps below:

Login using putty to your VPS server, And if you use local installation on your system, then just open the command line tool.

01. Make a file that will be used for swap:

$ sudo su
$ cd 

$ sudo fallocate -l 1G /myswapfile

02. To prevent regular users from writing or reading the file, set the file permissions to 600:

$ sudo chmod 600 /myswapfile

Next, type this command to verify the reserved space:

$ ls -lh /myswapfile
OUTPUT
linuxsubject@root:~$ sudo fallocate -l 1G /myswapfile
linuxsubject@root:~$ ls -lh /myswapfile
-rw-r--r-- 1 root root 1.0G March 21 10:10 /myswapfile
linuxsubject@root:~$

03. Create a Linux swap area on the file:

$ sudo mkswap /myswapfile
OUTPUT
Setting up swapspace version 1, size = 1 GiB (1048576 bytes)
no label, UUID=fde7d2c8-0c56a-456a-9027-fw731d2ab4z3

04. Run the following command to activate the swap file:

$ sudo swapon /myswapfile

To make the change permanent, edit the /etc/fstab file as follows:

$ sudo nano /etc/fstab

and paste the following line:

/myswapfile swap swap defaults 0 0

05. Check that the swap is active using the swapon or free commands, as shown below:

$ sudo swapon --show
OUTPUT
NAME      TYPE      SIZE  USED PRIO
/myswapfile file        2G    0B   -1

06 Tuning your Swap Settings

The swappiness the parameter controls how frequently data is moved from RAM to the swap area by your system. This is a proportion represented by a number between 0 and 100.

Values near “0” prevent the kernel from swapping data to the disk unless it is strictly essential. Values closer to 100 will attempt to place more data into the swap in order to clear up more RAM capacity. More swappiness values mean more CPU usage, and if you have 1 Core CPU and you set swappiness value between 70 and 80 then your CPU usage graph will be 100% with warnings, and due to higher CPU usage maybe your Database shutdown. It mostly happens on VPS servers. So, we need to set swappiness value around 0 to 20. A swappiness level of 60 is not a bad number for a desktop. You might want to move it nearer to 0 for a server (Any Linux based server like Debian 10, 11, or Ubuntu 22.04, 22.10, etc.)

By typing: we can see the current swappiness number.

$ cat /proc/sys/vm/swappiness
Output
60

Let’s change swappines value. Official Ubuntu Community article in which professionals recommend set swappines to 10. (Read article https://help.ubuntu.com/community/SwapFaq)

sudo sysctl vm.swappiness=10

This configuration will stay in place until the following reboot. By including the following line in our /etc/sysctl.conf file, we can set this value automatically upon restart:

$ sudo nano /etc/sysctl.conf

Add the following at the bottom:

vm.swappiness=10

Save the file by pressing Ctrl + S, then Ctrl + X.

Adjust the Cache Pressure

The vfs_cache_pressure variable, which will prioritize inode and dentry information over other data, will then be modified. The system’s preference for caching inode and dentry information over other data is configured by this parameter.

$ cat /proc/sys/vm/vfs_cache_pressure
OUTPUT
linuxsubject@root:~$ cat /proc/sys/vm/vfs_cache_pressure
100

The inode information will be removed from the cache by our system because the setup is already complete. Set the sysctl vm.vfs_cache_pressure cache pressure to a more reliable value by doing as follows:

$ sudo sysctl vm.vfs_cache_pressure = 50
OUTPUT
vm.vfs_cache_pressure = 50

Once more, this is only applicable for the current session. In the same way that we did with our swappiness option, we can modify it by adding it to our configuration file:

$ sudo nano /etc/sysctl.conf

At the bottom, add the line that specifies your new value:

vm.vfs_cache_pressure=50

Save the file by pressing Ctrl + S, then Ctrl + X.

Conclusion

On your Ubuntu 20.04 system, we demonstrated how to create a swap file as well as activate and configure swap space.

In Linux, what is the recommended swappiness value?

More swappiness values mean more CPU usage, and if you have 1 Core CPU and you set swappiness value between 70 and 80 then your CPU usage graph will be 100% with warnings, and due to higher CPU usage maybe your Database shutdown. It mostly happens on VPS servers. So, we need to set swappiness value around 0 to 20. A swappiness level of 60 is not a bad number for a desktop. You might want to move it nearer to 0 for a server (Any linux based server like Debian 10, 11 or Ubuntu 22.04, 22.10, etc.)

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *