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.

Frequently Asked Questions

How do you clear swap space in Linux using Ubuntu 22.04?

To clear swap space in Linux using Ubuntu 22.04, you can follow the steps like checking the swap & RAM state, turning off the swap, and then turning it back on.

What is swap space and why is it important for system performance?

Swap space is a part of secondary memory used with main memory to improve system performance. It helps handle inactive processes when memory is full, preventing system slowdowns.

What command can be used to check memory and swap details in Ubuntu 22.04?

In Ubuntu 22.04, you can use the 'free -h' command to check memory and swap details. It provides information on memory usage and swap space utilization.

How can turning off swap space affect system performance?

Turning off swap space can impact system performance negatively as it may lead to memory issues when handling multiple tasks concurrently.

What are the steps to free up swap space in Ubuntu 22.04?

To free up swap space in Ubuntu 22.04, you can turn off the swap using 'sudo swapoff -a' command, check swap and RAM state, and then turn the swap back on.

How does insufficient swap space affect system responsiveness?

Insufficient swap space can cause system responsiveness issues, leading to slow performance and potential system crashes when memory and swap space are fully utilized.

Why is it important to optimize system resources like swap space?

Optimizing system resources such as swap space is crucial to enhance system performance, especially during multitasking, by ensuring efficient memory management and process handling.

What are the consequences of having full swap space on Ubuntu 22.04?

Having full swap space on Ubuntu 22.04 can slow down the system, impact multitasking capabilities, and lead to performance issues due to the lack of available memory for active processes.