If you’re a Linux newbie and you want to install any Linux OS like Ubuntu Desktop or Ubuntu Server, RedHat Server or even CentOS or Fedora or Mint, you always allocate some space for ‘/’ and ‘swap’ before installing. We all know why we need ‘/’. But why do we need a swap memory? What are its uses? What happens when you don’t have a swap memory? In this post, we are going to learn what is Swap space in Linux and why we need it.
What is Swap space?
When your RAM or physical memory is full, Swap memory is used. This is the most common explanation for you get when you ask What is Swap memory? Although it’s true, the above sentence can be interpreted in more than one ways. Some users might think Swap is extended RAM memory. If your RAM memory is full it runs applications using Swap memory. This is where the confusion arises. Linux does not use Swap memory as RAM.
To understand Swap, you need to first understand how Linux uses physical memory. Linux divides its RAM into various small memory called as ‘pages‘. When your RAM is full, Linux uses swap, not to run other applications, but to move some of the pages into Swap memory to clear up and make some space in RAM.
The least often used page goes from RAM to Swap so that it can run the applications smoothly. In short, Linux stores its least often used pages from RAM to Swap to free up memory in RAM. If the kernel needs the pages again, it directly takes it from the Swap memory.
Although, Swap memory does help the RAM to free up space, it cannot be considered as a replacement for RAM. All it does is store pages that the RAM doesn’t need now but might need in the near future.
A lot of times, an application will use a lot of pages while starting up but not later on. Such pages can later be moved into swap memory and free up RAM for other applications. So Swap memory is not extended RAM memory but is a good ally of physical RAM which keeps pages it might need later.
Do you need a Swap memory?
So now that we know what swap memory is, do you really need it? Well, the answer depends on the amount of RAM you have. If you have 1GB or 2 GB of RAM, you definitely need a swap memory! This guy didn’t have swap memory in his PC with 1GB of RAM.
If you have more than 3GB of Physical memory and you’re not a fan of multi tasking, you don’t need a swap memory.
However, a Swap memory is always recommended.
Is Swap memory always useful?
As helpful as swap memory might sound like, it also has some downfalls. Swap memory is in your hard-disk which is your secondary memory. Secondary memory is way slower than your Primary memory (RAM). The speed of operations performed on RAM can be calculated in nanoseconds whereas disks need milliseconds, which are thousands of times slower. Excessive swapping of pages from RAM to swap back and forth or from disk to swap memory will slow down your machine. In such cases, adding more RAM is the only solution.
How much Swap memory do you need?
Now you know what is Swap, what happens when you don’t have a Swap memory and its disadvantages. As mentioned earlier, it is always recommended to have a swap partition. So how much memory should you allocate to your Swap partition?
If (RAM <= 2GB)
Swap = RAM * 2;
else
Swap = RAM + 2;
Follow the above condition while allocating your swap memory.