How to add swap space on Ubuntu 20.04?

Overview

The operating system can store data that it is momentarily unable to hold in RAM in a designated area of hard drive storage called swap. Apps running out of memory may be avoided by boosting the server's swap capacity. With a few limitations, this enables you to enhance the volume of data that your server can store in working memory. When RAM is no longer big enough to hold the data for running applications, the swap space on the hard disc will be mostly used. This guide will show you how to add swap space on Ubuntu 20.04.

Prerequisites

There are certain prerequisites that need to be met before you begin:

  • Ubuntu version 20.04 or any other Linux equipped system

  • A regular user (non-root) having sudo privileges

  • Internet connection

Step 1: Verifying Swap Information in the System

We may check to see whether the system has any swap space present before we start. Although it is possible to have more than one swap file or swap partition, in most cases one should suffice.

  • If there is a configured swap in the system, we may check it by typing:

sudo swapon --show

If you receive no output, your system doesn't presently have any open swap space.

  • You can confirm that there is no active swap using the below command:

free -h

There is no active swap on the system, as can be seen in the Swap row of the output.

Step 2: Examining the Hard Drive Partition's Available Space

  • We'll examine our current disc utilisation to make sure we have adequate space before we build our swap file. Run the below command to examine the current disc utilisation:

df -h

Here our disc is the highlighted one with / in the Mounted on column.

Although there are many differing viewpoints regarding the ideal swap space size, it ultimately comes down to your preferences and the needs of your particular application. In general, an amount that is equal to or twice as much as the RAM on your machine is a decent place to start.

Step 3: Create a Swap File

We can now make a swap file on our filesystem since we know how much hard drive space is available. In our root (/) directory, we will allot a file with the desired size that we will call swapfile.

  • The fallocate command is the most effective tool for generating a swap file. A file of the requested size is immediately created by this command.

Note: Since the server in our example has 1G of RAM, the 1G file we create in this guide will be 1G in size.

sudo fallocate -l 1G /swapfile
  • By entering the following command, we can confirm that the proper amount of space was set aside:

ls -lh /swapfile

Step 4: Alter the Swap file permissions

We now need to actually convert this file into swap space as it is the right size.

The file's permissions must first be restricted so that only users with root rights may see its contents. This limits access to the file for common users, which has important security ramifications.

  • Use the following command to alter the file's permissions so that only "root" may access it:

sudo chmod 600 /swapfile
  • Verify the file permissions that were assigned:

ls -lh /swapfile

As you can see, the read and write flags are only enabled for the root user.

  • Use the below command to designate the file as swap space.

sudo mkswap /swapfile
  • We may enable the swap file after marking it, allowing our system to begin using it:

sudo swapon /swapfile
  • Type the following to see if the swap is available:

sudo swapon --show
  • To confirm our conclusions, we may review the output of the free tool once more:

free -h

Our swap has been successfully configured, and our operating system will start using it as needed.

Step 5: Making the Swap File Permanent

The swap file is now active for the current session thanks to adjustments we recently made. On the other hand, the server won't automatically keep the swap settings after a reboot. This may be changed by including the swap file in the /etc/fstab file.

  • In case something goes wrong, take a backup of the /etc/fstab file:

sudo cp /etc/fstab /etc/fstab.bak
  • Enter the following code at the end of your /etc/fstab file to add the swap file information:

echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

After that, we'll explore several options that may be modified to optimise our swap space.

Step 6: Optimizing your Swap Settings

You may change a few settings that will affect how well your system handles swap in terms of speed.

Modifying the Swappiness Property

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

Values nearer 100 will attempt to store more data in swap in an effort to preserve more free RAM space. This could be preferable in some circumstances, depending on the memory profile of your apps or the purposes for which you use your server.

  • To view the swappiness value right now, enter the following:

cat /proc/sys/vm/swappiness

A setting of 60 swappiness is not a bad number for a desktop. For a server, it might be better to move it nearer to 0.

Using the sysctl command, the swappiness can be changed to a different value.

  • Enter the following to increase the swappiness to 10:

sudo sysctl vm.swappiness=10

This configuration will stay in place until the following reboot.

sudo nano /etc/sysctl.conf

Add the following line at the bottom of /etc/sysctl.conf file to adjust this value automatically upon restart:

vm.swappiness=10

When you're done, save and shut the file.

Modifying the Cache Pressure Setting (Optional)

You might also want to change the vfs_cache_pressure setting, which is related. This configuration determines how much the system will prioritise caching inode and dentry data over other data.

Essentially, this is information regarding filesystem access. This is a great thing for your system to cache because it's often quite expensive to search up and very frequently requested. Inquiring about the proc filesystem once again will get the current value:

  • Type the below command to check cache pressure setting:

cat /proc/sys/vm/vfs_cache_pressure

Our system eliminates inode information from the cache too promptly as it has now been set up.

  • Type the below command to set this to a more restrained value, like 50.

sudo sysctl vm.vfs_cache_pressure=50
  • Once again, this applies only to the current session. As with our swappiness setting, we can modify that by including it in our configuration file:

sudo nano /etc/sysctl.conf

Add the line with your revised value at the bottom:

vm.vfs_cache_pressure=50

When you're done, save and shut the file.

Conclusion

If you follow the instructions in this manual, situations that would often result in out-of-memory exceptions will be given some flexible space. Some of these typical issues can be greatly reduced by using swap space.

The best remedy if you are experiencing OOM (out of memory) issues or discover that your system cannot perform the apps you require is to optimise your application setups or update your server.

Last updated