Swapping & Overcommit
Virtual Memory Advanced · Memory & Systems track
7 min read
In this module
When Physical Memory Runs Out
Physical RAM is finite, but the total virtual memory all your processes have allocated can far outstrip it. The kernel papers over the gap with swapping (moving pages to disk), overcommit (allowing more allocation than physical memory), and page replacement (deciding which pages to kick out). Knowing how these knobs work is the difference between confidently running production workloads and watching the dreaded OOM killer take out your database at 3am.
Linux lets processes allocate more virtual memory than physical RAM exists, gambling that not all of it will be touched at once. When the gamble fails, swap absorbs the spillover — and when swap fills, the OOM killer picks something to kill.
Swap Space
Swap space is disk space the kernel borrows as an extension of physical RAM. When physical memory is full and someone wants more, the kernel picks infrequently-used pages and writes them to swap, freeing the physical frames. When a swapped-out page gets accessed again, a major page fault fires: the kernel reads the page back from swap into RAM (potentially swapping something else out to make room).
Swap can be a dedicated partition or a swap file:
# Check current swap usageswapon --show
# Create and enable a swap filedd if=/dev/zero of=/swapfile bs=1G count=4chmod 600 /swapfilemkswap /swapfileswapon /swapfileSwap is dramatically slower than RAM. Even SSDs have access times around 100 microseconds — a thousand times slower than DRAM's ~100 nanoseconds. A workload that actively swaps will hit a performance cliff. Swap is a safety net, not a performance feature.
Page Replacement Algorithms
When the kernel needs a free physical page and there are none to be had, it has to evict an existing one. The evicted page either gets written to swap (if dirty) or simply dropped (if clean and backed by a file). The choice of which page to kick out directly shapes performance.
LRU (Least Recently Used): evict the page that's gone untouched the longest. Real implementations approximate it, since true LRU tracking is too expensive to maintain.
Linux uses a two-list LRU scheme: an active list of recently accessed pages and an inactive list of eviction candidates. Pages climb from inactive to active when touched, and slide back to inactive when they go cold. The kernel evicts from the tail of the inactive list.
The kernel determines whether a page has been accessed by checking the accessed bit in the page table entry, which the CPU sets automatically when the page is read or written. The kernel periodically clears accessed bits and checks them later; pages with cleared bits that are not re-set have not been accessed and are good eviction candidates. This is called the "clock" or "second chance" algorithm.
Put the two lists and the accessed bit together and a page's life looks roughly like this (the real kernel has more refinements, but the shape holds):
The "second chance" is the step from Inactive back to Active. A page drifting towards the tail of the inactive list gets rescued if anything touched it in the meantime, so only pages that really have gone cold reach eviction.
The OOM Killer
When the system runs out of both physical memory and swap, the kernel's OOM (Out Of Memory) killer picks a process to murder, freeing its memory for everyone else. It's a last resort, and its appearance means the system has been promised more memory than it can possibly deliver.
The OOM killer scores each process on:
- Memory usage: bigger consumers earn higher scores, because more memory gets freed by killing them
- oom_score_adj: a tunable per-process value (-1000 to 1000). Critical services set this to -1000 to become immune; less important processes set higher values to volunteer themselves first.
# Check a process's OOM scorecat /proc/$(pidof myapp)/oom_score
# Protect a critical process from OOM killingecho -1000 > /proc/$(pidof critical_service)/oom_score_adjThe OOM killer fires SIGKILL — uncatchable, unignorable — at the chosen process. It dies immediately, with no chance to clean up. If the victim had unsaved state, you may end up with data corruption.
Memory Overcommit
Linux's default memory allocation policy is overcommit: malloc() and mmap() succeed even when there isn't enough physical memory to back the allocation. The kernel bets on a few things: programs allocate more than they actually use (sparse data structures, lazy initialisation), fork() doubles virtual memory but rarely uses the copy (COW dodges the actual duplication), and not every program touches all its allocations at once.
The overcommit behaviour is controlled by /proc/sys/vm/overcommit_memory:
- 0 (heuristic): the default. The kernel applies heuristics to decide whether an allocation is "reasonable." Very large allocations may get refused.
- 1 (always): the kernel stops doing commit accounting and pretends there's always enough memory. malloc() can still return NULL if the process runs out of address space or hits a limit such as
ulimit -v, but never because RAM is short. The OOM killer settles that bill later. - 2 (never): the kernel tracks committed memory and rejects allocations that would push the total past the commit limit: swap plus
overcommit_ratiopercent of physical RAM (50% by default), or swap plusovercommit_kbytesif you set that instead. The current limit shows up asCommitLimitin/proc/meminfo. malloc() returns NULL once it's reached.
Mode 2 (strict accounting) makes the global OOM killer far less likely, because user-space allocations can't be promised beyond the limit. It doesn't retire it: the kernel's own allocations and stack growth sit outside that accounting, a high overcommit_ratio can still promise more than exists, and a cgroup that hits its own limit gets its own OOM kill whatever the global mode. The cost is that programs may fail to allocate even when physical memory is technically available — because other programs have reserved but not used their allocations.
Memory Pressure and cgroups
In containerized environments, each container has a memory limit enforced by Linux cgroups (control groups). When a container's processes exceed their cgroup memory limit, the kernel either: reclaims memory within the cgroup (evicting file cache and swapping), or invokes the cgroup-level OOM killer (killing a process within the container, not system-wide).
# Set a 512MB memory limit for a cgroup (cgroup v2, the default on current distributions)mkdir /sys/fs/cgroup/mycontainerecho 512M > /sys/fs/cgroup/mycontainer/memory.maxHow eagerly the kernel swaps anonymous memory, rather than dropping file cache, is tuned by vm.swappiness (default 60; lower values favour keeping anonymous pages in RAM). Docker and Kubernetes use cgroups to enforce memory limits. The memory.max (cgroups v2) or memory.limit_in_bytes (cgroups v1) setting caps the container's physical memory usage. The memory.swap.max setting controls swap usage. Memory pressure metrics (memory.pressure, part of the kernel's Pressure Stall Information, which also reports system-wide figures in /proc/pressure/memory) show how much time tasks spend stalled waiting for memory, enabling orchestrators to take proactive action (scaling up, migrating workloads) before the OOM killer strikes.
Understanding these kernel mechanisms is essential for operating production systems. Monitoring swap usage, setting appropriate OOM scores, configuring overcommit policy, and tuning cgroup memory limits prevent both performance degradation and uncontrolled process termination.