What is using swap in linux

Linux Swap Size Recommendation – Best Practices

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 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 space 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 and degrade performance by swapping out processes.

Читайте также:  Linux name resolution failed

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 32GB 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 Size Should Be? – Recommendation

Now coming to the main question. How much Linux Swap 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, Ubuntu’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
Читайте также:  Установка postgre на linux

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 swap 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 Size Matter?

This is a complicated question as it completely depends on a user’s workload. If you consider hibernation, the swap 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.

Conclusion

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.

Читайте также:  Линукс перезагрузка комбинация клавиш

FAQs

What is the size of Swap in Linux?

According to Fedora, the size of Swap in Linux should 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)

How to set swap size in Linux?

Swappiness can have a value between 0 to 100. A value of 0 turns off the swappiness and a value from 1 to 100 instructs the kernel to swap out processes. You can use the following command to set a swappiness value and adjust the parameter on a small scale to find an optimal value.
sudo sysctl vm.swappiness=[VALUE_0_TO_100]
e.g.
sudo sysctl vm.swappiness=10
Update the /etc/sysctl.conf file to make it persistent.
sudo nano /etc/sysctl.conf
Write this at the end, and save it.
vm.swappiness=10

Do I need Swap with 32GB RAM?

For general purposes, you don’t need Swap with 32GB RAM. If you do RAM-intensive works that require more RAM, having a Swap partition will save you from unwanted crashes.

Does 8GB RAM need swap space?

You should use at least 3GB of swap space if your system has 8GB of RAM.

How much swap space for 4GB RAM?

If your system has only 4GB of RAM, you should have a swap space of 2GB or more to avoid any unwanted system crashes.

Can swap be larger than RAM?

There is no added benefit of having a swap larger than RAM. Swap uses SSD or HDD space that is much slower than your RAM. More swap space will not increase performance.

Why use swap Linux?

You should use a swap to avoid any unwanted system crashes. Linux swap can improve system performance.

Can I install Linux without swap partition?

Linux can be installed with or without a swap partition. You can turn it on or off at any time after installation.

Is swap partition necessary with SSD?

Swap partition is not necessary to run Linux. But having a swap partition will increase system performance and avoid unwanted crashes. As SSDs are faster than HDDs, having a swap file or partition on an SSD will increase performance due to faster speed.

Is swap partition better than file?

Swap partition and swap file are the same thing. The only difference is, a swap partition is a separate partition on an SSD or HDD, and a swap file is a file saved in a directory. There is no added benefit to either of them.

Источник

Оцените статью
Adblock
detector