Fundamentals 12 min read

How Linux Swaps Memory: Inside Swap, Swappiness, and kswapd Mechanics

This article explains Linux’s swap subsystem in the 4.4 kernel, covering what swap does, the role of swappiness, memory watermarks, kswapd’s operation, swap partition priority, and how the kernel balances anonymous page swapping with file‑cache reclamation.

ITPUB
ITPUB
ITPUB
How Linux Swaps Memory: Inside Swap, Swappiness, and kswapd Mechanics

Introduction

The Linux swap code has evolved from early 2.6 kernels to version 4.6, with this article focusing on the swap implementation in the 4.4 kernel. Swap is a small but essential part of the kernel’s complex memory‑management system. After reading, you will understand what swap does, how swappiness influences it, what memory watermarks are, when kswapd triggers swapping, and the purpose of swap‑partition priority.

What is Swap?

Swap refers to a dedicated partition or file used to move pages out of RAM when memory is scarce. You can list active swap areas with swapon -s:

Filename      Type      Size   Used   Priority
/dev/dm-4     partition 33554428 0    -1

When memory pressure arises, the kernel swaps out pages to avoid out‑of‑memory (OOM) conditions. Swap usage is tightly coupled with the kernel’s memory‑reclamation mechanisms.

Memory Reclamation

The kernel reclaims memory for two main reasons: to keep free pages available for sudden allocation requests, and to free pages occupied by the page cache to improve I/O efficiency. Two mechanisms are employed:

kswapd : a kernel thread that periodically scans memory zones to maintain a healthy free‑page pool.

Direct page reclaim : invoked when an allocation request cannot be satisfied from free pages, triggering an immediate reclamation path.

Both paths eventually call shrink_zone(), which iterates over LRU (least‑recently‑used) lists defined in mm/vmscan.c:

#define LRU_BASE 0
#define LRU_ACTIVE 1
#define LRU_FILE 2

enum lru_list {
    LRU_INACTIVE_ANON = LRU_BASE,
    LRU_ACTIVE_INACTIVE = LRU_BASE + LRU_ACTIVE,
    LRU_INACTIVE_FILE = LRU_BASE + LRU_FILE,
    LRU_ACTIVE_FILE = LRU_BASE + LRU_FILE + LRU_ACTIVE,
    LRU_UNEVICTABLE,
    NR_LRU_LISTS
};

The kernel scans four primary LRU lists: inactive/active anonymous pages and inactive/active file‑backed pages. Anonymous pages are reclaimed via swapping, while file‑backed pages are reclaimed by writing back dirty data (if any) and freeing clean pages.

Purpose of Swappiness

The /proc/sys/vm/swappiness parameter (default 60, range 0‑100) controls how aggressively the kernel prefers swapping anonymous pages versus reclaiming file cache. A higher value makes the kernel more willing to swap; a lower value favors cleaning file cache first.

Internally, the kernel uses the get_scan_count() function (called from shrink_lruvec()) to decide the scanning priority based on swappiness. The relevant snippet is:

/*
 * With swappiness at 100, anonymous and file have the same priority.
 * This scanning priority is essentially the inverse of IO cost.
 */
anon_prio = swappiness;
file_prio = 200 - anon_prio;

When swappiness is 100, both anonymous and file pages are treated equally. The default 60 biases the kernel toward freeing file cache (which often incurs less I/O) before swapping.

Global Reclaim and High Watermark

Even with swappiness set to 0, the kernel will still swap if memory is critically low. During a global reclaim, the kernel checks whether the sum of free pages and file pages in a zone falls below the high‑watermark threshold. If so, it forces anonymous‑page reclamation:

if (global_reclaim(sc)) {
    unsigned long zonefile = zone_page_state(zone, NR_ACTIVE_FILE) +
                             zone_page_state(zone, NR_INACTIVE_FILE);
    unsigned long zonefree = zone_page_state(zone, NR_FREE_PAGES);
    if (unlikely(zonefile + zonefree <= high_wmark_pages(zone))) {
        scan_balance = SCAN_ANON;
        goto out;
    }
}

This logic ensures that when both free memory and reclaimable file cache are insufficient, the kernel prioritizes swapping anonymous pages to avoid OOM.

Conclusion

Linux’s swap subsystem balances two reclamation strategies—swapping anonymous pages and cleaning file‑backed cache—using the swappiness setting and zone watermarks. Understanding the LRU lists, kswapd behavior, and the code paths in mm/vmscan.c helps developers tune memory usage and diagnose performance issues related to swapping.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

kswapdswappiness
ITPUB
Written by

ITPUB

Official ITPUB account sharing technical insights, community news, and exciting events.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.