Your system uses Random Access Memory (aka RAM) when it runs an application. When there are only a few applications running your system manages with the available RAM.
But if there are too many applications running or if the applications need a lot of RAM, then your system gets into trouble. If an application needs more memory but entire RAM is already in use, the application will crash.
Swap acts as a breather to your system when the RAM is exhausted. What happens here is that when the RAM is exhausted, your Linux system uses part of the hard disk memory and allocates it to the running application.
Why is swap needed?
There are several reasons why you would need swap.
- If your system has RAM less than 1 GB, you must use swap as most applications would exhaust the RAM soon.
- If your system uses resource heavy applications like video editors, it would be a good idea to use some swap space as your RAM may be exhausted here.
- If you use hibernation, then you must add swap because the content of the RAM will be written to the swap partition. This also means that the swap size should be at least the size of RAM.
- Avoid strange events like a program going nuts and eating RAM.
Do you need swap if you have lots of RAM?
This is a good question indeed. If you have 32GB or 64 GB of RAM, chances are that your system would perhaps never use the entire RAM and hence it would never use the swap partition.
But will you take the chance? I am guessing if your system has 32GB of RAM, it should also be having a hard disk of 100s of GB. Allocating a couple of GB of swap won’t hurt. It will provide an extra layer of ‘stability’ if a faulty program starts misusing RAM.
Do you need swap if you have lots of RAM?
If you have 32GB or 64 GB of RAM, chances are that your system would perhaps never use the entire RAM and hence it would never use the swap partition.
But will you take the chance? I am guessing if your system has 32GB of RAM, it should also be having a hard disk of 100s of GB. Allocating a couple of GB of swap won’t hurt. It will provide an extra layer of ‘stability’ if a faulty program starts misusing RAM.
Can you use Linux without swap?
Yes, you can, especially if your system has plenty of RAM. But as explained in the previous section, a little bit of swap is always advisable.
How much should be the swap size?
Different people have a different opinion on ideal swap size. Even the major Linux distributions don’t have the same swap size guideline.
If you go by Red Hat’s suggestion, they recommend a swap size of 20% of RAM for modern systems (i.e. 4GB or higher RAM).
CentOS has a different recommendation for the swap partition size. It suggests swap size to be:
- Twice the size of RAM if RAM is less than 2 GB
- Size of RAM + 2 GB if RAM size is more than 2 GB i.e. 5GB of swap for 3GB of RAM
Ubuntu has an entirely different perspective on the swap size as it takes hibernation into consideration. If you need hibernation, a swap of the size of RAM becomes necessary for Ubuntu.
Otherwise, it recommends:
- If RAM is less than 1 GB, swap size should be at least the size of RAM and at most double the size of RAM
- If RAM is more than 1 GB, swap size should be at least equal to the square root of the RAM size and at most double the size of RAM
- If hibernation is used, swap size should be equal to size of RAM plus the square root of the RAM size
Confused? I know it is confusing. This is why I have created this table that will tell give you the Ubuntu recommended swap size based on your RAM size and hibernation need.