What is Swap in Ubuntu?

Published: October 12, 2022

What is Swap in Ubuntu?

Before we delve deeper in Swap, we thought it would be good to first cover Physical and Virtual Memory. Physical memory is the Random Access Memory (or, RAM) available on our System. To view how much RAM is available to us, we can use command-line utility, free. Open a terminal and issue the following -

free -m

In the output, you would see Mem & Swap. Mem displays us the information about RAM while Swap is for Swap memory. Apart from the Physical memory i.e. RAM we have additional memory which we refer to as Swap. It resides on the physical disk and can be made available through a partition or file.

What is Swap in Ubuntu?

So, the virtual memory is a combination of these two i.e. Physical memory and Swap. Furthermore, Virtual Memory consists of fixed-length blocks, which are also referred to as pages. Now, processes require memory space to function and it is allocated in the form of pages.

The amount of RAM available to our system is finite and if it crosses a certain threshold then, without Swap our processes would begin to fail. But, with Swap - inactive pages would begin to move into Swap. Thus, it frees up the RAM or Physical memory and let the processes function. In addition to, when required the inactive pages in Swap are moved out to RAM.

But, one thing we need to keep in mind that - Swap should not be considered an alternative to RAM. It just isn't as fast as RAM. All it does is rescue our system when things start to go haywire. Our CPU can process data from physical memory (or, RAM) much faster.

To an extent, with the help of Swap, we can run resource hungry applications. Ideally, if we are running out of memory then it is better to prioritize your work and close applications which are not needed. That way, we can reduce the extra load on our system.

In conclusion, Swap comes to rescue when Physical memory isn't capable enough to handle process requests. Neither we ignore Swap nor it should be considered an alternative to RAM.