Linux swappiness is a kernel parameter that defines how aggressively the system will utilize swap space relative to physical RAM. This setting, exposed through the proc filesystem, directly influences memory management behavior by determining the likelihood the kernel will move inactive pages from RAM to disk. The default value is typically set to 60, indicating a balanced approach, but understanding its mechanics is essential for optimizing performance in specific environments.
Understanding the Linux Virtual Memory Subsystem
The concept of swappiness cannot be fully grasped without first understanding the Linux virtual memory manager. The kernel constantly juggles between keeping data in fast physical RAM and moving less frequently accessed data to slower disk-based swap. RAM is finite, and applications often request more memory than is physically available; this is where the interplay between the page cache, inactive lists, and swap space becomes critical. Swappiness is the dial the system administrator uses to fine-tune this balancing act.
How Swappiness Values Affect System Behavior
The value assigned to swappiness is an integer between 0 and 100. A higher value, such as 100, makes the kernel very aggressive in swapping data out of RAM as soon as it is deemed inactive, prioritizing keeping active applications in physical memory. Conversely, a value of 0 does not disable swapping entirely; rather, it tells the kernel to avoid swapping unless absolutely necessary, such as when memory is critically low and the system risks crashing. Values between these extremes adjust the probability the kernel will select a swap page over reclaiming memory from the page cache.
Performance Implications for Desktops
For desktop users, a lower swappiness value often results in a more responsive experience. Because desktop environments frequently involve interactive applications that users switch between quickly, keeping those applications in RAM avoids the latency associated with reading data from a hard drive or SSD. Setting the value too high might cause the system to swap out a game or a browser tab prematurely, leading to stuttering or delays when returning to it. A setting between 10 and 30 is often recommended for general desktop use to ensure active tasks remain in memory.
Optimizing for Servers and Databases
Server environments, particularly those running databases like MySQL or PostgreSQL, require a different approach. These applications often manage large datasets that exceed available RAM, relying on the page cache for performance. A lower swappiness value is generally preferred here to prevent the kernel from prematurely evicting the valuable disk cache. Maintaining the disk cache is crucial because reading data directly from RAM is significantly faster than calculating it again from the underlying storage blocks. Values ranging from 1 to 10 are common in these scenarios to protect the cache.
Viewing and Modifying the Current Setting
The current swappiness value can be viewed by reading the appropriate file in the /proc filesystem. This allows for immediate inspection without rebooting. Temporary changes can be applied on the fly using the sysctl command, which is useful for testing different configurations. However, to ensure the setting persists across reboots, the value must be added to the system configuration file. The following commands demonstrate how to view, apply temporarily, and apply permanently.