DSA, Database System & Operating System โ Memory Management, File Systems & Administration, NEC licence examination syllabus (Nepal Engineering Council).
Swapping and Managing Free Memory Space
Moving whole processes to disk when memory runs short, and tracking which holes are free.
๐ Where this lives: when a machine "starts thrashing" and everything becomes unusable while the CPU sits at 5%, you are watching this topic fail. The classic symptom is that the disk light is solid and the mouse stutters โ the system is spending all its time moving pages rather than executing. The modern form is different but the same idea: macOS compresses pages instead of writing them out, and Linux has zram. Both trade CPU for I/O because CPU got cheap and disks did not. Search "thrashing working set thrashing point".
Swapping
SWAPPING moves an entire process out of main memory to a
BACKING STORE (disk) and later brings it back.
SWAP OUT memory โ disk (a process is suspended)
SWAP IN disk โ memory (it is resumed)
This produces the two extra states of the seven-state process
model: SUSPENDED READY and SUSPENDED BLOCKED. The MEDIUM-TERM
scheduler makes the decision.
WHY IT WORKS: execution-time binding. Because the MMU
translates addresses at every access, a process can be brought
back at a DIFFERENT physical address with no change to its
code. With compile-time binding it would have to return to the
exact same place.
THE COST โ and it is why swapping whole processes is obsolete:
transfer time = process size / transfer rate
WORKED EXAMPLE: a 100 MB process, disk transfer 50 MB/s
swap out = 100 / 50 = 2.0 seconds
swap in = 100 / 50 = 2.0 seconds
total = 4.0 seconds per swap
Four seconds of pure overhead. For a time quantum measured in
milliseconds that is unthinkable โ the process would spend
1000ร longer being moved than running.
On an SSD at 2 GB/s: 100/2000 = 0.05 s each way, 0.1 s total.
Better by 40ร, and still 100 ms of doing nothing.
CONCLUSION: swapping whole processes is only viable under
severe memory pressure. Modern systems SWAP PAGES instead
(paging), because moving 16 KB costs microseconds rather than
seconds.
WHAT MODERN SYSTEMS ACTUALLY DO:
ยท PAGING โ move individual pages, not whole processes
ยท COMPRESSION โ compress inactive pages in RAM rather than
writing them out. macOS does this by default; Linux offers
zram/zswap. Compressing 16 KB costs a few microseconds of
CPU versus ~100 ยตs of SSD I/O, so it wins whenever the data
is compressible.
ยท MEASURED ON THIS MACHINE: vm_stat reports
Pages free 4,171
Pages active 201,820
Pages inactive 199,306
Pages wired 152,264
at 16 KB per page that is
free 0.06 GB (65 MB โ almost nothing spare)
active 3.08 GB
inactive 3.04 GB
wired 2.32 GB
The INACTIVE list is the swap candidate pool โ pages not
recently used, kept in memory until something needs the
frame. WIRED pages cannot be swapped at all (kernel data,
real-time buffers).
Tracking free memory
The OS must know which memory is free. Two representations:
BIT MAP (bit vector)
one bit per allocation unit: 0 = free, 1 = allocated.
unit size 4 KB, memory 1 GB
units = 1 GB / 4 KB = 262,144
bitmap = 262,144 bits = 32 KB (0.003% overhead)
โ tiny, and trivially easy to find a free unit
โ finding n CONSECUTIVE free units is a bit-scan
โ the scan is O(n) in the worst case
WORKED, at this machine's 16 KB page size:
16 GiB / 16 KiB = 1,048,576 pages
bitmap = 1,048,576 bits = 128 KiB
โ 128 KB to track 16 GB. Negligible.
LINKED LIST of free holes
each free block holds its size and a pointer to the next.
โ no space wasted on allocated regions
โ allocation strategies (first/best/worst fit) operate
directly on it
โ finding n consecutive units means walking the list
โ the list nodes live in the free memory itself, so a wild
pointer write corrupts the allocator
COALESCING โ the essential operation:
when a block is freed, MERGE it with adjacent free blocks.
Without coalescing, memory degrades into a large number of
small holes and external fragmentation becomes fatal.
before free(B): [free 20K][B 30K][free 10K]
after free(B): [free 60K] โ coalesced
This is why a free-list node stores its size: you must know
where a block ends to test whether the next one is free.
THE BUDDY SYSTEM โ a compromise used in the Linux kernel:
ยท memory is split into power-of-two sized blocks
ยท to satisfy a request, repeatedly halve a block until the
smallest power of two that fits is reached
ยท each half is the other's "buddy"; when both are free they
merge automatically
WORKED EXAMPLE โ a 1 MB region, request 70 KB:
1024 KB split โ 512 + 512
512 KB split โ 256 + 256
256 KB split โ 128 + 128
128 KB โฅ 70 KB โ ALLOCATE 128 KB
internal fragmentation = 128 โ 70 = 58 KB wasted (45%)
โ coalescing is O(1): a buddy's address differs by exactly
one bit, so it is found by XOR rather than by searching
โ fast allocation and free
โ internal fragmentation up to nearly 50% โ as the 58 KB
above shows
Linux uses the buddy system for physical page allocation and
a SLAB allocator on top of it for small objects, precisely
because the buddy system alone wastes too much on small
requests.
Thrashing
THRASHING is the state where a system spends more time paging
than executing.
THE MECHANISM โ and note that it is a feedback loop, which is
why it collapses so suddenly:
1. memory is overcommitted; page faults rise
2. the CPU is idle waiting for page-in I/O
3. the OS observes low CPU utilisation and concludes it
should INCREASE the degree of multiprogramming
4. more processes are admitted, each needing frames
5. every process now has fewer frames โ MORE faults
6. go to step 2
The OS's own corrective action makes it worse. That is why
CPU utilisation does not degrade gracefully โ it falls off a
cliff.
CPU
util โค โญโโโโโโโฎ
โค โญโโฏ โฒ
โค โญโโฏ โฒ โ thrashing begins
โค โญโโฏ โฒ
โคโญโโฏ โฒ___________
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโบ degree of
multiprogramming
THE WORKING SET MODEL โ the standard defence.
W(t, ฮ) = the set of pages referenced in the last ฮ
references (the "working set window")
ยท |W| approximates how many frames the process actually
NEEDS right now
ยท sum the working-set sizes of all processes:
D = ฮฃ |W_i| (total demand)
ยท if D > available frames โ THRASHING is inevitable
ยท the OS then SUSPENDS a process, freeing its frames for the
rest. Reducing multiprogramming is the correct response โ
the opposite of what the naive feedback loop does.
CHOOSING ฮ matters:
too small โ misses part of the true locality
too large โ includes pages no longer needed
PAGE-FAULT FREQUENCY (PFF) โ a more direct control:
measure each process's fault rate.
rate too HIGH โ give it more frames
rate too LOW โ take frames away
If no frames can be freed, suspend a process. This controls
the symptom directly rather than estimating the working set.
LOCALITY OF REFERENCE is why any of this works:
TEMPORAL a page referenced now is likely referenced again
soon
SPATIAL a page near one referenced now is likely
referenced soon
Without locality, the working set would be the whole program
and virtual memory would be useless. Measured evidence of
locality appears in the next topic: a second pass over 64 MB
produced ZERO page faults because everything was already
resident.
The thrashing feedback loop is the most important idea here, and it generalises well past memory. A system that responds to overload by admitting more work will collapse rather than degrade โ which is the same failure as a web server whose retry logic amplifies a slowdown into an outage. The correct response to overload is always to shed load, not to accept more.
๐ Go further: Linux's answer to running out of memory is the OOM killer, and its heuristic is worth knowing because it decides which process dies. Each process gets a badness score from its memory footprint, adjustable via /proc/<pid>/oom_score_adj โ and the biggest consumer usually loses, which is why a database is often killed instead of the leaking process that caused the problem. In containers, cgroups memory limits make this per-container, which is what produces the "OOMKilled" status every Kubernetes user has seen. Search "Linux OOM killer oom_score_adj".
๐ก Exam angle: define swapping and compute transfer time = size / rate โ that is a guaranteed numerical, and note it doubles for out-and-in. Explain why swapping requires execution-time binding. Compare bitmap versus linked list for free-space tracking, and compute a bitmap's size for a given memory and unit size. Know the buddy system with a worked split and its up-to-50% internal fragmentation. The highest-value item is thrashing: describe the feedback loop, and state that the fix is the working-set model or page-fault frequency, both of which reduce multiprogramming.
Syllabus points
Swapping
Contiguous allocation: first/best/worst fit (numerical)
Fragmentation
Create a free account to tick topics off, take notes as you read, watch the video lessons and get a day-by-day study plan built around your exam date.
Related topics in Memory Management, File Systems & Administration