People get confused when it comes to Linux Swap. Many questions may arise in your head.
- What is Linux Swap? What is it used for?
- How much Linux Swap Space should I need?
- I have plenty of RAM on my system. Should I need Linux Swap?
- And lots more other questions.
These are the most frequently asked questions when choosing Linux Swap size. But no matter what size you choose, it is always recommended to have a Linux Swap. It will help you in the long run.
What Is Linux Swap?
Computers use RAM (Random Access Memory) to run applications. More running applications need more RAM to operate. A computer with 1 GB or less RAM is not capable enough to do many tasks simultaneously. Considering the modern work case scenario, any computer with less than 4GB of RAM will suffer from memory shortage. In this case, swap memory comes into play.
Swap memory is a breather to your computer when your system memory (RAM) is exhausted. During a Linux installation, it will use a small amount of your storage as a swap partition or swap file. This swap will act as RAM when you run out of system memory.
That means, adding more swap memory will increase performance, right? WRONG!
Look, adding more swap memory will not benefit you at all. Swap memory will only be used when it is needed.
When Is Swap Used In Linux?
Random Access Memory (aka RAM) is required to run every application. Swap on the other hand acts like RAM when the system memory is exhausted. As long as your system memory is available, chances are your swap memory is not being used.
But it depends on the swappiness of your system. If you set the swappiness value to aggressive, your system will thrash processes to the swap memory. Aggressive swappiness will help you free your RAM but also degrade performance by swapping out processes.
You need a balance between these two. The default swappiness value is 60 in most of the Linux distributions. But you can change it according to your need from 0 to 100. 0 will disable swappiness and 1 is the minimum value. 100 will swap out processes too aggressively and degrade the performance. The recommended value is 10 if you want to use most of your RAM and swap when necessary. But it will always depend on your workload.
When the RAM hits the swappiness value, the kernel starts swapping out idle processes to free up the RAM. This will increase performance and give you room to complete memory-hungry tasks without crashing. When you need the idle process, the kernel will swap the process back into the RAM.
Do You Need Swap Space For Your Linux System?
It is a simple question with a complicated answer. As far as we know, having a swap will benefit the overall system performance, but it is not mandatory. If your computer has 32 GB or more RAM, you may not need any swap unless you use hibernation. Storages like HDD and SSD are cheaper nowadays, so why not have some swap space? It will not cost much and your computer will have some extra room to handle unwanted memory consumption.
How Much Swap Space Size Should Be?
Now coming to the main question. How much Linux Swap Space Size should you use?
And the answer is, that there is no definite answer to this question. There are just recommendations, different thoughts, and opinions from different people.
Even the major Linux distributions like RedHat, Fedora, CentOS, and Ubuntu do not carry similar rules. They all have different work cases and different opinions.
RedHat suggests that having an amount of 20% of the RAM as Swap is a good idea.
Fedora has a different opinion on the swap space size. It suggests swap space size to be:
- 2x of RAM if RAM is less than 2 GB (i.e. 4GB of swap for 2GB of RAM)
- Size of RAM + 2 GB, if RAM size is more than 2 GB (i.e. 5GB of swap for 3GB of RAM)
Ubuntu on the other hand has a completely different approach as it considers hibernation. If you need hibernation, a swap size equal to the RAM size becomes necessary for Ubuntu.
Otherwise, Ubunut’s recommendations are:
- If RAM < 1GB Then Swap Size = RAM Size, Maximum Swap Size = 2X of RAM Size
- If RAM > 1GB Then Swap Size (Without Hibernation) = √RAM or Swap Size (With Hibernation) = RAM Size + √RAM, Maximum Swap Size = 2X of RAM Size
I know what you are thinking. It is confusing. Maybe the table below will help you out.
RAM No hibernation With Hibernation Maximum 256MB 256MB 512MB 512MB 512MB 512MB 1024MB 1024MB 1024MB 1024MB 2048MB 2048MB RAM No hibernation With Hibernation Maximum 1GB 1GB 2GB 2GB 2GB 1GB 3GB 4GB 3GB 2GB 5GB 6GB 4GB 2GB 6GB 8GB 5GB 2GB 7GB 10GB 6GB 2GB 8GB 12GB 8GB 3GB 11GB 16GB 12GB 3GB 15GB 24GB 16GB 4GB 20GB 32GB 24GB 5GB 29GB 48GB 32GB 6GB 38GB 64GB 64GB 8GB 72GB 128GB 128GB 11GB 139GB 256GB 256GB 16GB 272GB 512GB 512GB 23GB 535GB 1TB 1TB 32GB 1056GB 2TB 2TB 46GB 2094GB 4TB 4TB 64GB 4160GB 8TB 8TB 91GB 8283GB 16TB
Can You Use Linux Without Swap Partition or Swap File?
Yes, you can use Linux without a swap partition or file. But why bother having a small amount of space as a swap? Storages are cheap now and allocating some space as a swap won’t hurt much. Hence it will increase the performance and help you tackle memory leaks. You may not need a swap if you have a large amount of RAM.
But if you have an older computer with 4GB of RAM or less, it will be beneficial if you use a swap. It will give an extra layer of stability.
Does Linux Swap Space Size Matter?
This is a complicated question as it completely depends on a user’s workload. If you consider hibernation, the swap space size should be equal to or higher than the RAM size. And if you don’t use hibernation, you can go either without a swap or use the table above as a reference to match your needs. But my recommendation is, that you should have a swap at least the size of your RAM. This will help you in the long run.
I hope you understand the concept of Linux Swap. You will find people who use and don’t use Linux Swap and both have their own opinions. But at the end of the day, you need to decide whether to go with a swap or without a swap depending on your workload. If you have a large amount of RAM (i.e 32GB or more), you may not need a swap. But if you have 4GB or 8GB of RAM, I recommend having a swap.
How much Linux Swap Space Size do you use? Let me know in the comment section below.
Leave a Reply