What is linux swappiness

How To Change The Swappiness Value On Linux? – Easy Guide

The Swappiness value is a part of the Linux Kernel. If you are a Linux user, you should have heard of Linux Swap space. During the Linux installation, a small amount of hard disk space is allocated as a Swap. This Swap partition or file acts like RAM when the system memory is exhausted. And the Swappiness Value determines when to use the swap space.

In this post, I am going over some of the basics of Linux Swap and the Swappiness Value. And also how you can change the Swappiness Value on Linux.

What Is Swap?

Linux Kernel divides its physical memory (a.k.a RAM) into chunks of memory called pages. When applications need more memory, these pages can be allocated to them. If your system is running out of physical memory, the Linux kernel starts swapping inactive pages to the swap partition. Swap is a preallocated hard disk space that is used to increase the virtual memory of your system.

Swap is a breather to your system when the RAM is exhausted. If you do not have a swap partition or file and you are running out of memory, the Linux OOM kicks in and starts killing processes to free up memory. Adding additional swap space will help you to avoid Out of Memory conditions.

But the biggest drawback of swap is the low performance. As HDD is 100,000 times slower and SSD is 1000 times slower than RAM, it is not possible to overcome the memory shortage problems with it. If you constantly run out of memory, you need to install additional RAM. Swap only helps your system be somewhat responsive. So that you can avoid out-of-memory conditions.

What Is Swappiness?

Swappiness is a Linux kernel’s preference that is used to swap out processes from RAM to the swap partition. It is a balance between RAM and Swap space. When your RAM reaches the swappiness value, the Linux kernel starts swapping out idle processes to the swap partition to free up the RAM for active processes. This will degrade performance but prevent your system from crashing.

Swappiness can have a value between 0 to 100. A value of 0 will disable swappiness and 1 is the minimum value. A value of 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.

What Does Swappiness Do?

Swappiness is a parameter for the Linux kernel. This parameter tells the Linux kernel when to swap out memory pages to the swap partition. Aggressive swappiness will massively degrade performance. If you want system responsiveness, you need to maximize the RAM usage and avoid swapping.

Читайте также:  Информация о процессоре линукс

A swap partition is slow and you should not use this most frequently. Lower the value according to your needs. If you are on an old computer with very little RAM (i.e. less than 4GB), you may need to increase the Swap Size and the swappiness value. If you have 32GB or more RAM, lower the swappiness value as much as possible to increase system responsiveness.

Why Should You Change The Swappiness Value?

Many Linux users do not care about the swappiness value. Some do not know what it is and some just don’t care about it. But there are some good reasons to change the swappiness value.

By default, the swappiness value is 60. And this value can work with most systems. But if you have a large amount of RAM (i.e. 16GB or higher), you should change the swappiness value. A higher swappiness value will swap out memory pages and degrade performance. If you have a large amount of RAM then you should lower the value below 60. Many Linux users including me suggest the value of 10.

Swappiness set to 10 will let your system use the RAM as much as possible. As RAM is much faster than a hard drive, the overall system performance remains snappy. But if your system has less than 1GB of RAM, then you should set the swappiness value a bit higher. This will prevent system crashes and improve performance.

How To Check For The Swappiness Value?

By default, major Linux distributions use the value of swappiness to 60. You can see the current swappiness value by using the following sysctl command.

Another command you can use to see the swappiness value is the following cat command.

How To Change The Swappiness Value?

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. A higher value will aggressively be swapping processes out of physical memory. A lower value will make the kernel avoid swapping as long as possible.

As accessing swap memory is considerably slower than the physical RAM, a lower swappiness value will most likely improve overall system performance. The default value of 60 is appropriate if you have less than 4GB of RAM. If you have more than 4GB of RAM, for a regular desktop installation, a value of 10 is recommended. For database servers, a value of 1 is recommended.

These are all recommendations. The optimal swappiness value depends on your system workload and the physical memory (RAM) of your system. 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

The above command will allow you to change the swappiness value at runtime. Use different values to find out the optimal performance. This command will not make the swappiness value permanent. Every time you reboot, the value will be changed back to the default. To make it permanent, open the file /etc/sysctl.conf with a text editor and change the value of the following entry vm.swappiness=[YOUR_VALUE]. (Add the line if it does not exist in the file).

Читайте также:  Permission denied ftp linux

You can use any text editor you want. If you would like to use gedit or xed, replace the nano with gedit or xed and update the /etc/sysctl.conf file to change the swappiness value permanently.

I am using nano here. Open the terminal and enter the above command.

Change The Swappiness Value - Step 1

Find the vm.swappiness parameter and change the value. If it is not available, write the parameter like this vm.swappiness=10. I am using 10 here as the swappiness value. You can use your value that suits your needs.

Change The Swappiness Value - Step 2

Save the file by pressing ctrl+o and Enter. And press ctrl+x to exit.

Change The Swappiness Value - Step 3

Conclusion

So we have learned what swappiness is, how it works, and how to change the Swappiness Value to increase performance. I hope you now have a clear knowledge about the swap and swappiness value.

If you like this tutorial, don’t forget to share it with your friends. And if you have any questions, you can ask me in the comment section below. I would love to answer your questions.

Источник

How To Change the Swappiness of Your Linux System

Memory is a critical part of any system. The Linux kernel knows this and tries to ensure there’s enough memory at the system’s disposal. One way the Kernel manages memory is by using a swap system to create additional memory space.

In this tutorial, we will talk about what a swap is, how it works, and finally, how to customize the swappiness of your Linux system.

What Is Swap?

A Swap system is defined as space allocated on the disk by the operating system and used when RAM is insufficient. In Linux, the Swap system can be a partitioned disk space dedicated to acting as a swap or a swap file. The swap system can also be a combination of both swap partition and swap files.

When a system is out of physical memory, the Kernel swaps inactive pages from the main memory into the swap system. That frees up some space in the main memory for use by other processes.

Although you can get away without a swap system, if you have a system with less than 4 GB of RAM or a system with critical performance requirements, a swap can come in very handy—and is recommended. Otherwise, if your system requires more memory than the system can provide, the system will crash, which can have grave repercussions.

NOTE: Do not assume that a swap system is a replacement or an alternative to additional memory. A Swap system is nothing more than a partition or a file stored in the disk; it cannot fully function as physical memory. If your system is always running out of sufficient memory, consider adding more physical memory because increasing your swap system size won’t help much.

Читайте также:  Pantum bm5100adn драйвер linux

What is Swappiness?

Swappiness is a kernel parameter that allows system administrators to adjust how often the system utilizes the swap system. Swappiness defines the relationship between swapping out inactive pages from the RAM to the swap system.

To avoid confusion, Here is the official swappiness definition from Linux manual pages.

“This control is used to define how aggressive (sic) the kernel will swap memory pages. Higher values will increase aggressiveness; lower values decrease the amount of swap. A value of 0 instructs the kernel not to initiate swap until the amount of free and file-backed pages is less than the high water mark in a zone.

The swappiness feature first appeared in Linux kernel version 2.6; it ranges from value 0 to 100. The higher the swappiness value, the more the inactive pages are swapped from main memory to swap space. Lower values will lead to inactive pages remaining in the main memory.

How to Display the Swappiness Value Of Your System

By default, major Linux distributions will have the value of swappiness set to 60. You can view the swappiness value of your system using the sysctl command.

If you do not have a sysctl command in your system, use the command below to install it on Debian based systems.

How to Change the Swappiness Value

The default swappiness value is usually the appropriate values for most use cases. However, you may want to adjust this value for various reasons.

There is no best or recommended swappiness value. However, you can use various tools such as free to monitor how your system memory performs and adjust the swappiness value until you find your system’s optimal value.

We can adjust the swappiness value by editing the configuration file. This method preserves the swappiness value even after a reboot.

To do this, open the file /etc/sysctl.conf with your text editor and change the value of the following entry vm.swappiness to your suitable value. (Add the entry if it does not exist).

To change your system’s swappiness value during runtime, use the sysctl command as shown: Change the swappiness value to fit your optimal value.

NOTE: Accessing swap space is considered much slower compared to accessing physical memory. Therefore, setting your swappiness value to 100 will not guarantee an increase in speed.

Conclusion

In this guide, we discussed what swap is and how it works. Using this guide, you can optimize your system space requirements to fit your usage needs.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list

Источник

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