Adding Swap Space on Ubuntu 20.04

Adding Swap Space on Ubuntu 20.04



What is Swap Space?

Swap Space is also known as Virtual Memory in modern Linux Systems. Swap Space in Linux is a space on the disk that is used when the amount of physical memory (RAM) is full. At the point when a system needs more memory resources (RAM is full), then the inactive pages in memory are moved from the RAM to the swap space.

Where swap space helps the machines with an insufficient amount of RAM, it should not be considered a replacement for more RAM storage. Swap space is located on the hard drives, which have a slower access time than the physical memory. The primary function of swap space is to act as a substitute disk space for RAM when more space is needed. For a system that continuously runs out of RAM, the best solution is to add more RAM.

Swap space can be a dedicated swap partition (recommended), a swap file, or a combination of swap partitions and swap files. Generally, when one is running Ubuntu on a virtual machine, a swap partition is not present, and the only option is to create a swap file.

Often, the size of the swap space is dependent on the size of the RAM in our system. It should generally be equal to twice our computer’s physical RAM for up to 2 GB of physical RAM. It should never be less than 32MB. According to the size of our RAM, swap space should be -

  • 2 times the amount of RAM for a system with less than 2 GB RAM.

  • Equal to the amount of RAM for a system with 2-8 GB RAM.

  • 0.5 times the amount of RAM for a system with 8-64 GB RAM.

  • Workload dependent for a system with more than 64 GB RAM.

Today's Agenda

In this post, we will learn how to add Swap Space on Ubuntu 20.04. We will know how to Create a Swap File, Adjust the Swappiness value and Remove a Swap File.

Prerequisite

This post has been prepared for the audience who : 
  1. Have access to a Linux-based system although the steps are almost similar in other platforms like CentOS or MacOS.
  2. Have a basic understanding of Linux based systems and their commands.
  3. Root user or user with sudo privileges to create and activate swap space.

Let's get started 

1. Creating a Swap File

We will take an example, wherein we will be creating a swap file of 2GB. If you want to create a swap file according to your requirement, simply replace the 2 GB with the desired value.

System Memory without adding Swap Space
System Memory without adding Swap Space

Step 1: Create a file to be used as a swap:

$ sudo fallocate -l 2G /swapfile

In case the above command throws an error - fallocate failed: Operation not supported, then you may use the following command -

 $ sudo dd if=/dev/zero of=/swapfile bs=1024 count=2097152

Step 2: Setting the file permissions to restrict regular users to read and write the file:

$ sudo chmod 600 /swapfile

Step 3: Create a Linux swap area on the file:

$ sudo mkswap /swapfile

OUTPUT -

Setting up swapspace version 1, size = 2GiB (2147479552 bytes) no label, 

UUID = b4a12ab8-92ef-4139-b3bd-e0fb9a4eb38f

Output after creating a Swap Space
Output after creating a Swap Space

Step 4: Activate the swap file:

$ sudo swapon /swapfile

To make the changes permanent, open /etc/fstab file and paste the following -

$ sudo nano /etc/fstab

 

/swapfile swap swap defaults 0 0 

Step 5: Verify the swap file is active:

$ sudo swapon --show

OUTPUT -

 

 Name Type  Size Used Prio

/swapfile  file 2G 0B -2

$ sudo free -h

OUTPUT -

 

             total        used        free      shared  buff/cache   available

Mem:      3888        167        3284       0       435       3492

Swap:      2047        0       2047



System Memory after adding Swap Space
System Memory after adding Swap Space

2. Adjusting the Swappiness value

Swappiness is the kernel parameter in the Linux system that defines how much and how often the kernel will use the swap space. The default value for Swappiness is 60, and it can range between 0 to 100. A higher swappiness value means that the kernel swaps more aggressively, and a lower value means that the kernel tries to avoid swapping as much as possible.

To check the current swappiness value, use the following command -

$ cat /proc/sys/vm/swappiness

OUTPUT -

60

To change the swappiness value, use the following command -

$ sudo sysctl vm.swappiness=25


This value is changed on your system immediately, but on reboot, the swappiness value will be reverted back to the default (60). To make permanent changes, navigate to the configuration file - /etc/sysctl.conf and add -

vm.swappiness=25


3. Removing a Swap File

In order to deactivate and delete a swap file, follow the steps below -

Step 1: Deactivate the swap space:

$ sudo swapoff -v /swapfile

Step 2: Remove the swap file entry from the /etc/fstab file:

  /swapfile swap swap defaults 0 0 

Step 3: Remove the actual swap file:

$ sudo rm /swapfile








Comments