Oct 13, 2023 3 min read

How to Change the Swappiness Value on Linux

Change the Swappiness Value on Linux with our step-by-step tutorial. Swap space is part of the hard disk used when the RAM memory is full.

Change the Swappiness Value on Linux
Table of Contents

Introduction

Before we begin talking about how to change the swappiness value on Linux, let's briefly understand – What is a swap space ?

Swap space is a component of the hard disk that is used when the RAM memory is full. A swap file or a separate swap partition can be used as the swap space.

Inactive pages are transferred from RAM to swap space when a Linux system runs out of physical memory.

Swappiness is a Linux kernel property that controls the balance between swapping out pages from physical memory to swap space and removing pages from the page cache. In essence, it establishes how frequently the system will utilize the swap space.

In this tutorial, you will change the swappiness value on Linux. We will also address a few FAQs on how to change the swappiness value on Linux.

Advantages of Swap Space

  1. Increased system stability: Swap space allows the system to handle excessive memory demands, preventing crashes or frozen applications.
  2. Optimized multitasking: Swap space enables efficient management of multiple running processes and prevents system slowdowns.
  3. Expanded virtual memory: Swap space extends the overall available memory capacity, accommodating larger applications and datasets.
  4. Improved performance: By offloading less frequently used data to the swap space, the RAM can focus on actively utilized information, enhancing overall system performance.
  5. Flexibility: Swap space provides flexibility in managing memory resources, allowing for easier adaptation to varying workloads and user requirements.

Checking the Swappiness Value

Use the cat command to check your system's current swappiness value:

cat /proc/sys/vm/swappiness

On the majority of Linux distributions, the default swappiness value is 60:

Output
60

While the swappiness value of 60 is acceptable for most users, in some instances, a lower value may be required.

You can also use the sysctl command to find the swappiness value:

sysctl vm.swappiness
Output

vm.swappiness = 60

Changing the Swappiness Value

Swappiness can range from 0 to 100 in value. The kernel is instructed to actively delay swapping out for as long as feasible when the value is 0. A 100 means that processes will be aggressively swapped out of physical memory.

A larger value will cause the kernel to use the swap space more aggressively, while a lower value will cause it to try to avoid swapping whenever feasible.

When compared to directly accessing physical memory, swap memory is substantially slower. The swappiness parameter's value should be decreased to enhance system performance. The suggested value for regular desktop installation is 10. For the majority of database servers, a value of 0 or 1 for swappiness is advised.

Your system workload and the size of RAM memory in your computer will determine the ideal swappiness value. To get the optimal value, modify this parameter in small increments.

For example, to set the swappiness setting to 10 at runtime, run the following command as root or sudo user:

sudo sysctl vm.swappiness=1

Open the /etc/sysctl.conf file in your text editor to make the swappiness parameter persistent across reboots:

sudo nano /etc/sysctl.conf

Find the vm.swappiness parameter and modify its value. Add the following line to the file if this parameter is missing:

vm.swappiness=1

FAQs to Change the Swappiness Value on Linux

Why would I want to change the swappiness value? 

Modifying the swappiness value allows you to control the aggressiveness of swapping and can optimize your system's performance based on your specific needs.

How can I change the swappiness value permanently? 

Add vm.swappiness=<value> to the /etc/sysctl.conf file. Replace <value> with the desired swappiness value (0 to 100). Reboot or run sysctl -p for the changes to take effect.

What is a recommended swappiness value? 

For most systems, a swappiness value between 10 and 30 strikes a balance between utilizing RAM effectively and avoiding excessive swapping.

Can I disable swapping altogether by setting swappiness to 0?

Setting the swappiness value to 0 doesn't disable swapping completely, but it reduces its frequency. Completely disabling swapping is not recommended as it may cause out-of-memory errors.

How can I determine the ideal swappiness value for my system? 

Experiment with different swappiness values to find the one that best suits your workload and provides optimal performance. Monitor system behavior and adjust accordingly.

Does changing the swappiness value require superuser (root) privileges? 

Yes, modifying the swappiness value requires superuser privileges. Use sudo or run the commands as root.

What are the potential consequences of setting a very low swappiness value? 

Setting a very low swappiness value may lead to out-of-memory errors if the system exhausts its physical RAM. Be cautious when configuring it too low.

Conclusion

You now know how to change the value of the swappiness parameter.

If you have any suggestions or queries, kindly leave them in the comments section.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Tutorials - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.