The swap space is the tiny part of the secondary memory that is used with the main memory to improve the performance of the system. The inactive processes are kept in a swap so that the memory has space for active processes. Sometimes it has been observed that due to multi-tasking, memory space and swap space are used completely. It may cause to slow down the system. In such a situation, users can free up swap space. If you don’t know how to do that, follow this guide to fix the stated problem on Ubuntu 22.04.

How Do You Clear Swap Space in Ubuntu 22.04?

The optimization of system resources can improve the performance of your system. Swap space is one of the system resources that play an important role when we are performing more than one task at a time. If your swap space is full due to a non-active process, you can free up it, in Ubuntu 22.04 by using the steps mentioned below. 

Step 1: Check the Swap & RAM State

If you want to clear the swap space in Ubuntu 22.04, firstly you should check the state of the swap and RAM. There should be a sufficient amount of memory space available to handle the inactive processes in swap.

The “free -h” command is a command used in Linux-based operating systems to check the use of memory and swap space. Use the command to check/test the memory and swap details:

$ free -h

The used swap is 2.01MB in our case.

Step 2: Turn Off the Swap

To clear the swap space, you need to turn off the swap. Use the below command to turn off the swap:

$ sudo swapoff -a

The command will turn off the swap service:

Step 3: Turn On the Swap

You can restart the service, by typing the following command in the terminal:

$ sudo swapon -a

The command will turn the swap service on:

Step 4: Verify the Clear Swap

Type the “free -h” command in the terminal again, and verify whether the swap is clear or not.

$ free -h

The output will display that the swap space has cleared. The used swap space is of 0B. The total and free space are equal. In my case, it is 2.1GB.

This is how you can clear the swap space for improving the performance of your system. 

Conclusion

To clear the swap space in Ubuntu 22.04, you can use the “swapoff -a” and “swapon -a” commands. You can see the details of the swap space by using the “free -h” command.  This Linux blog has thoroughly explained how you can clear the swap space in Ubuntu 22.04.