🐧 Linux Server Performance Optimization: Enhancing Memory Management Efficiency with Swappiness Settings

Hello! Today, we’re going to explore a truly useful memory management tip for operating Linux servers: configuring the vm.swappiness parameter.

Have you ever experienced your server suddenly slowing down or becoming unresponsive, perhaps with plenty of RAM but constant hard disk activity? In such cases, it’s worth checking your swappiness settings.

Through this post, I’ll explain what Swappiness is and how you can easily and thoroughly optimize it for your server environment! πŸš€

Understanding: What is vm.swappiness? πŸ€”

Let’s compare a computer’s memory (RAM) to a ‘desk’ and swap space to a ‘drawer cabinet’.

  • RAM (Desk): A fast and efficient space for tasks that need immediate processing.
  • Swap (Drawer Cabinet): A slower but ample space where documents not currently in use are temporarily stored when the desk is full. (Typically uses a portion of an HDD or SSD)

vm.swappiness is a value that determines how often and aggressively the Linux kernel uses this ‘drawer cabinet’ (Swap). It can be set from 0 to 100.

  • Higher value (Default: 60): Even if there’s some space on the desk (RAM), documents that “might be needed later” are proactively moved to the drawer cabinet (Swap). Very aggressive.
  • Lower value (e.g., 10): The system resists using the drawer cabinet (Swap) until the desk (RAM) is almost completely full. Very passive.

On modern servers with ample RAM, there’s no real need to frequently use Swap, which relies on slower disk access. Especially for services where memory access speed is critical, like database servers, minimizing Swap usage is beneficial for performance.

Checking Current Settings πŸ”

So, how is our server currently configured? You can easily check it with the following command in the terminal.

sysctl vm.swappiness

Alternatively, you can directly query the /proc file system.

cat /proc/sys/vm/swappiness

Most likely, the default value of 60 will be displayed.

# Example output
vm.swappiness = 60

Now, let’s change this value to 10 to minimize Swap usage and maximize RAM utilization.

Proceeding with Configuration Changes πŸš€

The configuration change involves two steps. First, making a permanent change so the value persists after reboot, and second, applying the change immediately without rebooting.

1️⃣ Making Permanent Changes (Persists After Reboot)

To ensure specific settings are loaded automatically every time the system boots, they must be recorded in a configuration file. We will create a new configuration file in the /etc/sysctl.d/ directory. This allows for clean management without touching the existing system configuration file (sysctl.conf).

Copy and paste the command below into your terminal.

echo 'vm.swappiness = 10' | sudo tee /etc/sysctl.d/99-swappiness.conf

Wait! Let’s understand what this command means in detail.

  • echo ‘vm.swappiness = 10’: Prints the text ‘vm.swappiness = 10’ to the terminal.
  • | (pipe): A conduit that passes the output (text) of the left command as input to the right command.
  • sudo tee [filename]: Executes the tee command with sudo (administrator) privileges. tee is a very useful tool that takes input and simultaneously displays it on the screen and saves it to a file. Here, it creates a file named /etc/sysctl.d/99-swappiness.conf and saves the text inside it.

πŸ’‘ TMI: Why not use sudo echo ‘…’ > [filename]? Redirection (>) is interpreted by the shell first, and at that point, sudo has not yet been applied, meaning it’s running with regular user privileges. This can lead to an error stating that you don’t have permission to write to system directories (/etc/…). sudo tee is the most reliable way to resolve such permission issues.

2️⃣ Applying Immediately Without Rebooting

The above setting will take effect from the next reboot. Since we want to see the effect immediately, we’ll use the command below to reload the system configuration files and apply them directly to the current kernel.

sudo sysctl -p

The -p option means to read and apply the configuration files (/etc/sysctl.conf, /etc/sysctl.d/*). When you execute the command, the vm.swappiness = 10 setting we just added will be displayed on the screen and applied.

Finally, Verify the Changes βœ…

All tasks are complete! Let’s double-check if the value has indeed changed.

sysctl vm.swappiness

Now, the result should display 10, as we intended.

# Output after change
vm.swappiness = 10

Congratulations! Your Linux server is now utilizing memory much more efficiently.

In Conclusion ✍️

Today, we learned about tuning the vm.swappiness value to enhance Linux server memory management efficiency. Although it’s a simple configuration change, it’s a crucial tuning point that can significantly impact server responsiveness and stability.

Of course, 10 isn’t the correct value for all servers. The optimal value can vary depending on the server’s role, memory capacity, and workload. Based on what you’ve learned today, finding the best value for your specific environment will be a valuable experience.


Tags: Linux, Server Management, Performance Optimization, Tuning, sysctl, vm.swappiness, Memory Management, Swap, DevOps, Ubuntu


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *