Linux Swappiness: Sysctl Swap Behavior Explained
The vm.swappiness sysctl parameter in the Linux
operating system controls the relative balance between reclaiming
anonymous memory pages to swap space and evicting file system page
caches. This article explains how the swappiness parameter functions
within the Linux virtual memory manager, what its numerical values
represent, how to adjust it, and how tuning this setting impacts system
performance and responsiveness.
The Role of Swappiness in Linux Memory Management
Physical memory (RAM) in Linux holds two primary types of pages:
- File-backed pages (Page Cache): Cached data read from disks or binaries. Reclaiming these pages is computationally inexpensive because the kernel can simply drop them; if needed again, they are re-read from storage.
- Anonymous pages: Memory allocated directly by active processes, such as stack and heap memory. To free RAM containing anonymous pages, the kernel must write them to disk in swap space, which requires active I/O.
The swappiness parameter, configured through
vm.swappiness, determines the kernel's preference when
system memory pressure occurs. It serves as a ratio weighing the cost of
reclaiming file-backed caches against the cost of swapping out anonymous
memory.
The Swappiness Scale
Swappiness accepts integer values, traditionally ranging from 0 to 100 (and up to 200 in newer Linux kernels supporting zswap or zram):
- 0: Instructs the kernel to avoid swapping anonymous pages for as long as possible. Swapping only occurs when the system is under extreme memory pressure to avoid Out-of-Memory (OOM) conditions.
- 1 – 10: Heavily favors keeping process memory in physical RAM and discarding file cache instead. This setting is common for low-latency database engines (such as MySQL, PostgreSQL, or Redis) where swapping causes unacceptable query delays.
- 60: The default value on most Linux distributions. It represents a balanced approach where the kernel actively reclaims both file cache and inactive anonymous pages to preserve a responsive balance of free memory.
- 100: Treats the reclamation of anonymous memory and file-backed memory with equal priority. The kernel aggressively swaps out unused process memory to maintain a large file system cache.
How to Check and Configure Swappiness
To view the current swappiness setting on a running system, execute:
sysctl vm.swappinessor read directly from the virtual file system:
cat /proc/sys/vm/swappinessApplying a Temporary Change
To change the value immediately without rebooting:
sudo sysctl vm.swappiness=10This takes effect instantly across the operating system but resets upon a reboot.
Applying a Permanent Change
To make the configuration persist across system restarts, add or update the parameter in the sysctl configuration:
- Open
/etc/sysctl.confor create a new file such as/etc/sysctl.d/99-swappiness.conf. - Add the line:
vm.swappiness = 10 - Apply the changes immediately:
sudo sysctl -p
Performance Considerations
Tuning swappiness depends entirely on system
workload:
- Desktop Systems: A value between 10 and 60 is standard. Lower values prevent UI freezes caused by desktop application memory being swapped out while running memory-heavy tasks.
- Database and Low-Latency Servers: Values between 1 and 10 prevent latency spikes by ensuring working data sets remain in physical RAM.
- High-Throughput File Servers: Values closer to 60 or higher allow the system to maintain a larger page cache, speeding up I/O read operations for connected clients.