Open In App

How to Change the Swappiness of your Linux system

Last Updated : 26 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will explore the crucial aspect of Linux system optimization by exploring the “swappiness” setting. Swappiness determines the system’s tendency to swap data between RAM and disk, influencing overall performance. This article not only explains the underlying mechanics of swappiness but also provides practical insights and examples for adjusting this parameter, both temporarily and persistently, to enhance Linux performance enabled to specific hardware configurations and workloads.

What is Swappiness?

Swappiness, in the context of Linux, is an important kernel parameter that governs the system’s propensity to transfer less actively used memory pages from RAM to disk swap space. Represented as a value between 0 and 100, swappiness determines the percentage of swapping activity. Higher values encourage more swapping, potentially enhancing system responsiveness, while lower values mitigate swapping, prioritizing the retention of data in physical RAM. With a default setting of 60 in most Linux distributions, indicating a preference to swap out up to 60% of inactive cached pages, users can fine-tune swappiness to align with specific workloads and available RAM, effectively minimizing I/O overhead stemming from excessive swapping.

How to Display the Swappiness Value of the System?

In this section, we will explore the steps to retrieve and display the current swappiness value of your Linux system. Utilizing the sysctl command, we can easily inspect the active swappiness setting, providing insights into the system’s memory management configuration.

Step 1: Check the Current Swappiness Setting ( sysctl )

The step consists of checking the current swappiness setting using the command sudo sysctl vm.swappiness. By default, major Linux distributions typically set the swappiness value to 60. This command provides a quick way to inspect and understand the active swappiness configuration on your system.

Command:

sudo sysctl vm.swappiness

Output:

Swappiness value of our system

Step 2: Check the Current Swappiness Setting (Procps)

In this step, the cat /proc/sys/vm/swappiness command is used to display the current swappiness value of the Linux system. The /proc/sys/vm/swappiness file contains the configuration parameter for swappiness, which determines how aggressively the system swaps out data from RAM to the swap space. The command simply outputs the numeric value representing the current swappiness setting.

Command:

cat /proc/sys/vm/swappiness

Output:

Swappiness Value using Procps

How to Change the Swappiness Value?

In the above section, we explored the steps to retrieve the Swappiness Value. In this section, we will navigate through the steps to change the Swappiness Value.

Step 1: Create the Entry of Suitable Value

Create or modify the entry for swappiness in the /etc/sysctl.conf file by using a text editor. Open the file with the command sudo gedit /etc/sysctl.conf and change the value of vm.swappiness to your desired setting. If the entry doesn’t exist, create it. This step ensures a persistent adjustment of the swappiness value on your Linux system.

Command:

sudo gedit /etc/sysctl.conf

Output:

Creating Entry

Step 2: Display the Created Entry

Verify the successful creation of the entry in the /etc/sysctl.conf file by using the command cat /etc/sysctl.conf | grep swappiness. This command displays the content of the file and filters for lines containing “swappiness,” confirming the presence of the newly created or modified entry.

Command:

cat /etc/sysctl.conf | grep swappiness

Output:

Verify Created Entry

Step 3: Change the Swappiness Value of System

Change the system’s swappiness value during runtime using the sysctl command. Execute the command sudo sysctl vm.swappiness=10 (adjusting “10” to your preferred value) to dynamically set the swappiness to the desired level. This allows testing and immediate application of the new swappiness value without requiring a system reboot.

Command :

sudo sysctl vm.swappiness=10

Output :

Swappiness Value Changed

Frequently Asked Questions on Changing Swappiness of Linux System – FAQs

What is swappiness on Linux?

Swappiness controls how Linux uses swap memory vs RAM for running programs and data. It’s a setting that can affect overall system performance.

What is the default swappiness value?

The default setting is 60. This means Linux will be aggressive about swapping less used memory pages from RAM onto disk.

Why would I want to change the swappiness value?

Lowering it from the default, say to 10, means Linux will try to avoid using swap and keep more stuff in memory. For desktops and general use, lowering it can speed things up.

How do I check my current swappiness value?

Use the command ‘cat /proc/sys/vm/swappiness’ to see your system’s current swappiness setting.

How do I temporarily change the swappiness?

Use ‘sudo sysctl vm.swappiness=10’ – but this will reset after restarting the computer.

Conclusion

In conclusion, swappiness is a Linux setting that controls how often the system swaps memory pages from RAM to disk. The default swappiness value of 60 tells Linux to be quite aggressive about using swap space. However, high disk I/O from excessive swapping can lead to performance issues. Lowering the swappiness allows Linux to favor using free RAM over swap, which is faster. The swappiness parameter tunes Linux memory management. Lower values lessen swap usage which usually speeds things up. Try values like 10 or adjust higher if your system lacks RAM. Changing it can optimize Linux to best suit your hardware.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads