X
X

Linux Swappiness: How Does Linux Decide When to Use Swap?

HomepageArticlesLinux Swappiness: How Does Linux Decide When t...

Linux Swappiness: How Does Linux Decide When to Use Swap?

Introduction

Many system administrators assume that using Swap means a server has run out of RAM. In reality, Linux memory management is more sophisticated than that.

Linux uses a kernel parameter called Swappiness to determine when it is appropriate to move less frequently used memory pages from physical memory (RAM) to Swap space—even if some RAM is still available.

Properly tuning Swappiness can significantly impact the performance and responsiveness of servers, especially for memory-intensive workloads.

What Is Swappiness?

Swappiness is a Linux kernel parameter that controls how aggressively the operating system prefers using Swap instead of keeping data in physical memory (RAM).

Its value ranges from 0 to 100:

  • Lower values make Linux avoid using Swap for as long as possible.
  • Higher values encourage Linux to move inactive memory pages to Swap earlier.

Swappiness does not force the system to swap immediately. Instead, it acts as one of the factors the kernel considers when making memory management decisions.

How Does Swappiness Work?

Linux continuously monitors memory usage and determines whether inactive memory pages should remain in RAM or be moved to Swap.

  • Low Swappiness values prioritize keeping application data in RAM.
  • High Swappiness values make the kernel more willing to swap inactive pages, freeing RAM for filesystem caches and active workloads.

The kernel makes these decisions dynamically based on memory pressure and workload characteristics.

When Should You Use Different Swappiness Values?

Low Values (10–20)

Recommended for workloads that require low latency, including:

  • Database servers
  • Web servers
  • Real-time or latency-sensitive applications

Keeping data in RAM minimizes swap activity and reduces response times.

Medium Values (30–60)

Suitable for:

  • General-purpose Linux systems
  • Most virtual machines
  • Standard application servers

These values provide a balanced approach between RAM usage and Swap utilization.

High Values (70–100)

Often appropriate for:

  • Desktop environments
  • Systems that benefit from maintaining a large filesystem cache
  • Workloads where maximizing available RAM for caching is more important than minimizing Swap usage

Benefits of Tuning Swappiness

Lower Application Latency

Reducing unnecessary swapping helps latency-sensitive applications maintain consistent performance.

Better Memory Utilization

Swappiness helps Linux balance the use of RAM and Swap based on the workload.

Improved System Stability

Proper tuning can prevent excessive memory pressure and reduce the likelihood of out-of-memory situations.

How Can You Check the Current Swappiness Value?

To view the current setting, run:

 
cat /proc/sys/vm/swappiness
 

To change it temporarily:

 
sudo sysctl vm.swappiness=10
 

To make the change persistent across reboots, add the following line to /etc/sysctl.conf (or an appropriate file under /etc/sysctl.d/):

 
vm.swappiness = 10
 

Then apply the configuration with:

 
sudo sysctl -p
 

Best Practices

  • Benchmark application performance before changing the Swappiness value.
  • Avoid disabling Swap completely unless you fully understand the implications.
  • Monitor RAM and Swap usage after making changes.
  • Choose Swappiness values based on workload characteristics rather than applying a universal configuration.

FAQ

Does a Swappiness Value of 0 Mean Linux Never Uses Swap?

Not exactly. A value of 0 makes Linux avoid using Swap whenever possible, but under severe memory pressure, the kernel may still use Swap to maintain system stability.

Is There an Ideal Swappiness Value?

No. The optimal setting depends on the workload, available RAM, application behavior, and performance requirements. Different environments may require different configurations.

Conclusion

Swappiness is an important Linux kernel parameter that influences how the operating system balances memory usage between RAM and Swap. Proper tuning can improve application responsiveness, optimize memory utilization, and enhance overall system stability. Rather than relying on a single recommended value, administrators should benchmark their workloads and select the Swappiness setting that best matches their production environment.


Top