Fundamentals 11 min read

Why Does Linux Need Swapping? Understanding Memory Pressure and Idle Page Management

Linux uses swapping to move rarely used memory pages to disk, alleviating memory pressure and reclaiming idle memory, but the performance impact of disk I/O can cause latency; this article explains the mechanisms, triggers, and kernel functions behind swapping, including direct reclaim, kswapd, and LRU lists.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Why Does Linux Need Swapping? Understanding Memory Pressure and Idle Page Management

Swapping is the Linux kernel mechanism that copies the contents of seldom‑used physical memory pages to a designated swap area on disk, freeing RAM for active processes. By extending virtual memory with swap space, the system can present the illusion of abundant memory, though disk I/O is orders of magnitude slower than RAM.

Linux supports two ways to provide swap space: a dedicated swap partition and a swap file within a regular filesystem. The swapon -s command lists active swap areas. Administrators typically size swap partitions based on workload—desktop systems may use twice the RAM size, while servers often disable or minimize swap and monitor performance closely.

Why Linux Needs Swapping

When memory is scarce, swapping moves rarely accessed pages to disk, instantly freeing RAM for the currently running process.

When memory is idle, swapping can relocate unused pages to disk, reserving RAM for future allocations by other processes.

Memory Shortage

When the demand for memory exceeds available RAM, the kernel performs a forced reclamation called Direct Page Reclaim . This is triggered during the __alloc_pages_nodemask call, which first searches the free page list and, if none are available, falls back to the slow‑path allocation functions.

static inline struct page * __alloc_pages_slowpath(gfp_t gfp_mask, unsigned int order, struct alloc_context *ac) {
    ...
    if (alloc_flags & ALLOC_KSWAPD)
        wake_all_kswapds(order, gfp_mask, ac);

    page = get_page_from_freelist(gfp_mask, order, alloc_flags, ac);
    if (page) goto got_pg;

    if (can_direct_reclaim && (costly_order || (order > 0 && ac->migratetype != MIGRATE_MOVABLE)) && !gfp_pfmemalloc_allowed(gfp_mask)) {
        page = __alloc_pages_direct_compact(gfp_mask, order, alloc_flags, ac, INIT_COMPACT_PRIORITY, &compact_result);
        if (page) goto got_pg;
        ...
    }

retry:
    page = __alloc_pages_direct_reclaim(gfp_mask, order, alloc_flags, ac, &did_some_progress);
    page = __alloc_pages_direct_compact(gfp_mask, order, alloc_flags, ac, compact_priority, &compact_result);
    page = __alloc_pages_may_oom(gfp_mask, order, ac, &did_some_progress);
got_pg:
    return page;
}

Wake the kswapd daemon, which attempts to obtain a page from the free list.

If the allocation is expensive, call __alloc_pages_direct_compact to compact memory and retry the free‑list lookup.

Invoke __alloc_pages_direct_reclaim to reclaim and allocate a new page directly.

Retry __alloc_pages_direct_compact to further compact memory.

Finally, call __alloc_pages_may_oom; if allocation still fails, the kernel may trigger an out‑of‑memory (OOM) kill.

These steps illustrate how Linux handles low‑memory situations through memory compression, direct reclamation, and OOM termination.

Memory Idle

Many applications allocate large amounts of memory at startup that remain unused later. The kernel’s kswapd daemon monitors low‑watermark thresholds ( WMARK_LOW, WMARK_HIGH, WMARK_MIN) and swaps out idle pages to keep enough free RAM for active processes.

Linux employs a Least Recently Used (LRU) algorithm with two primary lists per memory zone: active_list (recently accessed pages) and inactive_list (candidates for reclamation). Pages are further classified by enum lru_list values such as LRU_INACTIVE_ANON, LRU_ACTIVE_ANON, LRU_INACTIVE_FILE, LRU_ACTIVE_FILE, and LRU_UNEVICTABLE. When a page is accessed, it moves to the head of the active list; kswapd balances the two lists by moving the oldest active pages to the inactive list, where shrink_zones reclaims them.

Summary

Swapping frees RAM by moving rarely used pages to disk, ensuring running processes remain functional when physical memory is insufficient.

Swapping also reclaims idle pages, allowing the system to reserve RAM for future allocations.

Administrators must tune swap size and parameters based on workload; for example, Kubernetes nodes typically disable swap.

References

Kubelet/Kubernetes should work with Swap Enabled #53533 https://github.com/kubernetes/kubernetes/issues/53533

Linux Performance: Why You Should Almost Always Add Swap Space https://haydenjames.io/linux-performance-almost-always-add-swap-space/

Do we really need swap on modern systems? https://www.redhat.com/en/blog/do-we-really-need-swap-modern-systems

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.

KernelLinuxOperating systemswap_spaceSwapping
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

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.