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:

  1. 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.
  2. 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):

How to Check and Configure Swappiness

To view the current swappiness setting on a running system, execute:

sysctl vm.swappiness

or read directly from the virtual file system:

cat /proc/sys/vm/swappiness

Applying a Temporary Change

To change the value immediately without rebooting:

sudo sysctl vm.swappiness=10

This 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:

  1. Open /etc/sysctl.conf or create a new file such as /etc/sysctl.d/99-swappiness.conf.
  2. Add the line:
    vm.swappiness = 10
  3. Apply the changes immediately:
    sudo sysctl -p

Performance Considerations

Tuning swappiness depends entirely on system workload: