Memory Management — Swapping, Contiguous Allocation & Fragmentation
Press Next → or use ← → arrow keys
The Story That Explains Memory Management
That is the whole drama of memory management: the attendant is the OS memory manager, cars are processes, bays are memory blocks — and the scattered empty bays are fragmentation.
The memory manager decides which process goes where in RAM, translates every address a program uses into a real hardware location, moves programs in and out of memory, and tries to keep the free space usable.
Address Binding — When Do Addresses Become Real?
Only if addresses are resolved at run time can the OS move a process to a different place in RAM — which is exactly what swapping and compaction require. That flexibility is worth doing a tiny translation on every single memory reference.
Logical vs Physical — Meet the MMU
The CPU emits a logical address. The MMU checks it against the limit register (so a process can't reach outside its space), then adds the relocation (base) register to get the real physical address. Logical 500 → physical 14,500. Step outside the limit and you get a segmentation fault.
Swapping — RAM ↔ Backing Store
To free RAM, the OS copies a process's image to a fast disk (the backing store) and later copies it back. At 50 MB/s, moving 100 MB out takes 2 s, and 100 MB back another 2 s — 4 s total, plus disk seek/rotation. That cost is why modern systems prefer paging small pieces over swapping whole processes.
Contiguous Memory Allocation
In contiguous allocation, each process gets a single unbroken region of RAM, described by its base and limit registers. The kernel sits in low memory; user processes stack above it, with free holes in between. Simple and fast — but those holes are where fragmentation begins.
First-Fit · Best-Fit · Worst-Fit
Given identical holes and requests, the three strategies place processes in different holes — and end with very different leftover space. The next slides run all three on the same numbers to see which wins.
Where Does the 315 KB Request Go?
Holes (address order): 200, 600, 300, 400, 100 KB. First request: 315 KB.
First-Fit grabs the first hole ≥ 315 (the 600). Best-Fit hunts for the tightest fit (the 400). Worst-Fit takes the biggest (the 600). Same request, three different placements — and they diverge further with each new request.
Full Run — Requests 315, 195, 450 KB
| Request | First-Fit | Best-Fit | Worst-Fit |
|---|---|---|---|
| 315 | hole 600 → 285 left | hole 400 → 85 left | hole 600 → 285 left |
| 195 | hole 200 → 5 left | hole 200 → 5 left | hole 400 → 205 left |
| 450 | largest 400 → FAILS | hole 600 → 150 left | largest 300 → FAILS |
| Served | 2 of 3 | 3 of 3 ✅ | 2 of 3 |
By spending its big 600 hole last — only when the 450 request truly needed it — Best-Fit satisfies all three. First-Fit and Worst-Fit both burn a big hole early and then can't fit the 450. But note: Best-Fit isn't always best — it tends to leave many tiny unusable slivers over time.
Fragmentation — The Silent Memory Killer
External fragmentation is those 60 scattered empty bays with no 6-in-a-row for the bus. The total free space is fine — it's just in the wrong shape. That's the problem compaction solves.
Compaction — Make One Big Hole
Compaction moves every allocated block to one end and coalesces the scattered free space into one big hole. Now the 400 KB request that couldn't fit anywhere (largest hole was 150) slots right in. The catch: it requires execution-time binding and is expensive — it copies gigabytes of RAM.
Fragmentation Analysis
Memory (750 KB total): 100 U · 50 F · 200 U · 30 F · 150 U · 80 F · 100 U · 40 F (U = used, F = free).
| Question | Working | Answer |
|---|---|---|
| Total external fragmentation | 50 + 30 + 80 + 40 | 200 KB |
| Fit a 100 KB request now? | largest free hole = 80 | No (80 < 100) |
| Fit a 200 KB request now? | largest free hole = 80 | No |
| After compaction? | 200 KB coalesced into one hole | Both fit ✓ |
| Memory used | 100 + 200 + 150 + 100 = 550 | 550 KB (73.3%) |
There's a full 200 KB free, but scattered across four holes of 50, 30, 80 and 40 — so even a 100 KB process can't be placed. This is external fragmentation in one number, and why compaction (or paging) exists.
Seven Rules for Memory Management
Sharing One RAM Among Many
From address binding and MMU translation, through swapping and contiguous allocation, to the three fit strategies, fragmentation and compaction — you can place processes in RAM, translate their addresses, and reason about wasted space.
Contiguous allocation's fragmentation problem is exactly what paging and segmentation were invented to solve — by letting a process's memory be non-contiguous. That's the next tutorial, leading into virtual memory.
🧠 End of tutorial · Press ← to review, or click Restart