How to Change the swappiness configuration? (Step 5)

Swappiness is a parameter that controls how aggressively your system uses swap space. Adjusting this value can help optimize performance depending on whether you prioritize RAM usage or swap space. This guide will show you how to check and change the swappiness value on CentOS/AlmaLinux.

Step 1: Check the Current Swappiness Value

Before making any changes, check the current swappiness value:

cat /proc/sys/vm/swappiness

The output will be a number between 0 and 100, representing the current swappiness value. The default is usually 30, which offers a balanced approach between using RAM and swap space.

You can also check the swappiness value with:

sysctl vm.swappiness

The output should look like this:

vm.swappiness = 30

Step 2: Temporarily Change the Swappiness Value

If you want to change the swappiness value temporarily (until the next reboot), use the following command:

sudo sysctl vm.swappiness=NEW_VALUE

Replace NEW_VALUE with the desired swappiness value:

  • For VPS: Set the swappiness value to 1 . This minimizes swap usage, keeping more data in RAM, which is important in environments with limited resources.
  • For Dedicated Servers: You can use 10 for a more balanced approach or keep it at 30 if swap usage is acceptable and you have ample RAM.

For example, to set the swappiness to 1:

sudo sysctl vm.swappiness=1

This change takes effect immediately but will be reset after a reboot.

Step 3: Permanently Change the Swappiness Value

To make the swappiness change permanent, follow these steps:

Open the sysctl configuration file:

sudo nano /etc/sysctl.conf

Add or modify the following line to set the desired swappiness value:

vm.swappiness = NEW_VALUE

For example, to set the swappiness value to 1 for a VPS:

vm.swappiness = 1

Save the changes and exit Nano by pressing Ctrl + O to write the file and Ctrl + X to exit.

Apply the changes with:

sudo sysctl -p

This command reloads the sysctl configuration and applies the new swappiness value permanently.

Step 4: Verify the Swappiness Change

To verify that the swappiness value has been changed successfully, check the current swappiness value again:

cat /proc/sys/vm/swappiness

The output should reflect the new swappiness value you set.

That’s it! You’ve successfully changed the swappiness value in CentOS/AlmaLinux. Adjusting swappiness helps optimize system performance based on your specific needs, whether you’re running on a VPS with limited RAM or a dedicated server with more resources.

Share