Ch. 21 · Operating Systems

Operating Systems: Swap and Thrashing

How swapping extends memory, when it becomes thrashing, and how overcommit and the OOM killer respond to exhaustion.

~2 min readintermediateupdated Oct 5, 2026

Swap lets the OS move pages that are not in active use to disk, freeing physical memory for other work. It extends the effective memory beyond RAM, but disk is orders of magnitude slower, so heavy swapping degrades performance badly and can spiral into thrashing.

Before you start

You should understand virtual memory and paging. This article covers swap behavior and its failure modes.

Step-by-step walkthrough

Step 1: Swap frees physical frames

When memory is tight, the OS evicts less-used pages to the swap area and marks them not present. Accessing an evicted page triggers a page fault, and the OS reads it back. A small amount of swapping is normal and cheap.

Step 2: Thrashing collapses performance

If the working set exceeds RAM, pages are evicted and faulted back repeatedly, and the CPU spends its time on page faults rather than work. This is thrashing, and it can make a machine that is “busy” effectively idle. The fix is to reduce the working set or add memory, not to tune swap.

Step 3: Know how the OS reacts to exhaustion

Linux overcommits memory, promising more than it has, and when allocation actually exceeds capacity the OOM killer terminates a process. This means a memory-hungry process may be killed rather than getting a clean allocation failure, so capacity planning and limits matter.

Worked scenario

The counters separate light swapping from thrashing.

vmstat 1 5    # watch si/so (swap in/out) and the fault rate
# sustained high si/so with low CPU use indicates thrashing
Terminal

Walk through the example

si and so show swap traffic; occasional small values are fine. Sustained high swap traffic with CPU mostly idle is the thrashing signature: the machine is spending time moving pages, not doing work. That points to a working set larger than RAM.

Common mistake

Relying on swap for performance, so a workload that fits RAM is sized to overflow it and thrashes. Another is ignoring overcommit, so a process is killed by the OOM killer and the cause looks like a mysterious crash.

Verify the behavior

Run a workload just under RAM and confirm swap stays idle. Increase it past RAM and observe growing swap-in/out and falling throughput. Set memory limits and confirm the OOM behavior when a process exceeds them.

Interview exercise

Is swap a way to run more work than fits in memory?

Answer and reasoning

It lets the system survive with more allocated memory than RAM, but not without a performance cost, because swapped pages are read back at disk speed. If the active working set exceeds RAM, the system thrashes and throughput collapses. Swap is a safety margin, not a capacity substitute.

Continue learning

Compare memory topics in Working set and thrashing and Virtual memory. Read the Linux vmstat documentation and try the Operating systems interview questions.

More in Operating Systems

read ✓Operating Systems · hard

Copy-on-Write Memory and fork

How copy-on-write lets fork share pages until a write, why RSS can be misleading, and the implications for memory and latency.

~2 min readread →
read ✓Operating Systems · mid

Operating Systems: Memory Allocation

Compare stack and heap allocation, how the allocator manages the heap, and the fragmentation and lifetime bugs that follow.

~2 min readread →
read ✓Operating Systems · hard

Operating Systems: CPU Cache Locality

Improve performance with spatial and temporal locality, avoid pointer chasing, and understand false sharing between threads.

~2 min readread →
esc