Clear swap memory space in Ubuntu distribution

We would discuss how to clear Swap memory space in Ubuntu distribution here. Every application we run on our system takes up some space on RAM. And, amount of space its going to occupy depends on various factors.

Now, consider a scenario wherein you run ‘n‘ number of applications. Everything would work fine as long as you have free space available on RAM. But, once these applications clogs up RAM then our system would start to slow down. And, this would continue till we free RAM space by closing applications which aren’t required.

In Ubuntu distribution, we have a substitute to RAM space. We call it Swap space. When our RAM gets clogged up, then kernel automatically detects which applications have not been in use or lying idle. Therefore, it shifts those applications to swap space thereby freeing up RAM. It also automatically shift applications back from swap to RAM as and when needed.

The swap space can be in form of a partition or a file. We will discuss it in detail in coming articles.

There are times when we need to clear swap space for various reasons. Swap can be disabled or enabled by using command-line utilities swapoff and swapon respectively. We assume here that you already have swap file/partition present on your distribution.

Note: Following operations would require you to have superuser privileges. In case you don’t have one, then contact your System Administrator for assistance.

Clear swap memory space in Ubuntu distribution

Although kernel does it on its own. Still, if we want we can use swapoff & swapon command-line utilities to disable and enable swap space briefly.

So, to disable swap, issue the following in terminal –

sudo swapoff -a

where,

-a option is used to disable swap space on all known swap devices i.e. partition or files likewise.

This would make swap space unavailable in our system. In turn, it clears up the swap space.

Now, we need to enable it back. Hence, issue the following command in terminal –

sudo swapon -a

where,

-a option is used to enable swap space on all know swap devices i.e. partition or files likewise.

In conclusion, we have discussed how to clear swap memory space in Ubuntu distribution.

Additional info

To display whether everything has worked as intended we can issue the following command –

swapon --show

--show option provides us the relevant details.

If it returns with nothing then swap space isn’t available.