Linux Kernel

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.

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.

sudo sysctl vm.swappiness

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

apt-get install procps

https://man7.org/linux/man-pages/man8/sysctl.8.html

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.

sudo sysctl vm.swappiness=10

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

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