What is swap usage in linux

Linux swap: what it is and how to use it

I mention Linux swap and swappiness in almost every article on things to do after installing Linux. You know that Linux swap is somehow related to RAM and swappiness can affect your system performance. It is time to explain what it is and how to use it in little more details.

Video

What is Linux Swap?

The Linux Kernel divides RAM into chunks of memories and the swapping process is when the Linux Kernel uses a hard disk space (swap space) to store information from RAM and thus releases some RAM space. That is why when you install a Linux distribution, the installation wizard usually asks you to assign some space for the system and another for the swap.

Using swap is a very useful way to extend the RAM because it provides the necessary additional memory when the RAM space has been exhausted and a process has to be continued. It is especially recommended when you have less than 1Gb of RAM. Although in the end, everything depends on you.

Do you need Linux Swap?

This is a question many novice users ask themselves when they begin to discover Linux. In fact, this will depend on the use and amount of RAM your computer has. Regarding the use, there are processes and applications that really use a lot of memory, for example, Google Chrome. However, most of the current equipment comes with at least 8Gb of RAM and that makes the swap process less necessary. Nevertheless, having a swap space is desirable even if you have a lot of RAM.

For example, usually, when your RAM gets full and the Linux kernel has no space to write into, your system will crash. On the other hand, if you have a swap space, it will be used by the Linux kernel and your system will keep working, though much slower. So, it is safer to have swap space.

Note: swap space has one disadvantage — it is much slower than RAM. So, adding a swap space will not make your computer faster, it will only help to overcome some limitations posed by RAM size.

Linux Swap Partition

I recommend that you create the swap partition during the installation of your Linux distribution. In general, these are the recommended sizes for the swap partition.

  • If your computer has 1Gb of RAM or less, then the swap partition should be twice the size of the RAM.
  • But, If you have between 2gb and 4gb of RAM, the size of the swap partition should be half the RAM.
  • Finally, If you have more than 4gb of RAM, then it is enough to have 2Gb.

But everything depends on your use case.

You can check the type and size of your swap with this command:

Читайте также:  Linux утилизация сетевого интерфейса

Swapon output

Above, I have a swap partition of 2Gb.

Linux Swap File

Alternatively, you can create a Linux Swap File after the installation. The modern Linux Kernel allows Swapping to a swap file instead of a swap partition. A swap file has an advantage over a swap partition that you can change the size of your swap any time easily by changing a swap file size.

If you want to create a swap file, run this command first:

sudo fallocate -l 1G /swapfile 

Note: this command is to create a 1Gb swap file. Replace 1G with the value you want.

Next, you have to set the correct permissions.

Then, format the file to swap.

Creating a Linux swap file

If you want the changes to be permanent, you need to edit the /etc/fstab file and add the following.

/swapfile swap swap defaults 0 0 

3. Add the linux swap file to the fstab

In the end, check the status of the swapfile:

4. Check the Linux swap file status

If you see the size of a swap file in the total column, you have done everything correctly.

How to remove a Linux Swap File

In case you need to remove a Linux swap file for any reason, you need to follow these steps.

First, deactivate the swap.

If you created the entry in the /etc/fstab file, remove it. To remind you, it is the line: /swapfile swap swap defaults 0 0 entry.

Finally, delete the actual Linux Swap File.

5. Removing a Linux swwap file

How to adjust the Swappiness value

Swappiness is a property of the Linux Kernel to define how often the swap space will be used. As you know RAM is faster than a hard drive. So, every time you need to use swap, you will notice that some processes and applications will run slower. However, you can adjust the system to use much more RAM than swap. This can help improve overall system performance.

Normally, the default swappiness value is 60. The smaller this value, the more of your RAM will be used.

To verify the swappiness value, run this command:

You should see the value of 60.

Show the swappiness default value

If you want to modify the default value, you need to edit the file /etc/sysctl.conf .

And add the following (10 is the most commonly recommended value):

Change the swappiness value

Save the file and close it with Ctrl+O and Ctrl+X shortcuts. In order to apply the changes, you need to reboot the system.

This way your Linux kernel will use more RAM and less swap, but it still will swap when your RAM memory gets critically full. Usually, this setting is recommended when you have more than 4Gb of RAM.

Conclusion

In conclusion, it is safer to have some swap space on your computer. You can use either a swap partition or a swap file. The latter becomes more and more common.

Linux swap is a technical concept but knowing how it works can improve the performance of the system. Just play with the swappiness value.

Do you have anything to add about Linux swap? Have you changed the default value of swappiness? Let me know in the comments.

Average Linux UserFollow I am the founder of the Average Linux User project, which is a hobby I work on at night. During the day I am a scientist who uses computers to analyze genetic data.

Источник

How Much Swap Should You Use in Linux?

How much should be the swap size? Should the swap be double of the RAM size or should it be half of the RAM size? Do I need swap at all if my system has got several GBs of RAM? Your questions answered in this detailed article.

Читайте также:  Изменение группы владельцев файла linux

How much should be the swap size? Should the swap be double the RAM size, or should it be half of the RAM size? Do I need swap at all if my system has got several GBs of RAM? Perhaps these are the most commonly asked questions about choosing swap size while installing Linux. It’s nothing new. There has always been a lot of confusion around swap size. For a long time, the recommended swap size was double the RAM size but that golden rule is not applicable to modern computers anymore. We have systems with RAM sizes up to 128 GB; many old computers don’t even have this much hard disk. But what swap size would you allot to a system with 32 GB of RAM? 64GB? That would be a ridiculous waste of a hard disk, won’t it? Before you see how much swap size you should have, let’s quickly learn a thing or two about swap memory. This will help you understand why swap is used. The explanation has been simplified for (almost) everyone’s understanding.

What is swap? When is swap used?

How much should be the swap size in Ubuntu and other Linux?

Your system uses Random Access Memory (aka RAM) when it runs an application. When only a few applications are running, your system manages with the available RAM. But if too many applications are running or the applications need a lot of RAM, your system gets into trouble. If an application needs more memory, but the 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. That sounds cool. Does this mean if you allocate 50GB of swap size, your system can run hundreds or perhaps thousands of applications simultaneously? WRONG! You see, the speed matters here. RAM access data in the order of nanoseconds. An SSD access data in microseconds, while a regular hard disk accesses the data in milliseconds. This means that RAM is 1000 times faster than SSD and 100,000 times faster than the usual HDD. If an application relies too much on the swap, its performance will degrade as it cannot access the data at the same speed as it would have in RAM. So instead of taking 1 second for a task, it may take several minutes to complete the same task. It will leave the application almost useless. This is known as thrashing in computing terms. In other words, a little swap is helpful. A lot of it will be of no good use.

Why is swap needed?

  • 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.
Читайте также:  Linux who are you

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.

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.

In fact, a distribution like Ubuntu automatically creates a swap file of 2 GB in size. This should give you an indication that having some swap space is recommended.

Don’t worry if you didn’t create a swap partition while installing Linux. You can always create swap file in Linux at any time.

How much should be the swap size?

Now comes the big question. What should be the ideal swap space for a Linux install?

And the problem here is that there is no definite answer to this swap size question. There are just recommendations.

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).

  • 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.

  • 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 give you the Ubuntu recommended swap size based on your RAM size and hibernation need.

RAM Size Swap Size (Without Hibernation) Swap size (With Hibernation)
256MB 256MB 512MB
512MB 512MB 1GB
1GB 1GB 2GB
2GB 1GB 3GB
3GB 2GB 5GB
4GB 2GB 6GB
6GB 2GB 8GB
8GB 3GB 11GB
12GB 3GB 15GB
16GB 4GB 20GB
24GB 5GB 29GB
32GB 6GB 38GB
64GB 8GB 72GB
128GB 11GB 139GB

How much swap size do you use?

The answer is never simple. As I stated earlier, for a long time, swap has been recommended to be double the size of RAM. In fact my Dell XPS 13 Ubuntu edition has 16GB of swap size for 8GB of RAM. So even Dell decided to go with the golden rule of swap=2xRAM.

What swap size do you prefer for your Linux system?

Источник

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