Operating System Slides 📂 Introduction · 15 of 22 39 min read

Memory Management in OS: Swapping, Contiguous Allocation & Fragmentation

Understand how the OS shares one physical RAM among many processes. This tutorial covers logical-to-physical address binding, the MMU with base and limit registers, swapping to a backing store, contiguous allocation with First/Best/Worst-Fit, and internal vs. external fragmentation — with animated diagrams and worked numericals.

🧠

Memory Management — Swapping, Contiguous Allocation & Fragmentation

How the OS shares one physical RAM among many processes: turning logical addresses into physical ones, swapping programs to disk, packing them contiguously — and fighting the fragmentation that packing creates. With animated diagrams.
Address Binding Swapping First/Best/Worst Fit Compaction

Press Next → or use ← → arrow keys

Section 01

The Story That Explains Memory Management

A mall has 500 bays and 2,000 shoppers arriving through the day. An attendant assigns each car a bay. As cars come and go, the lot ends up looking like a comb — a used bay, two empty, five used, one empty. By afternoon there might be 60 empty bays total… but nowhere are there 6 adjacent for a tour bus.

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 Core Job

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.

Section 02

Address Binding — When Do Addresses Become Real?

🔨
Compile Time
absolute code
Addresses are fixed when the program is compiled. Move it in memory and you must recompile — rigid, rarely used today.
📥
Load Time
relocatable code
The compiler emits relocatable addresses; the loader fixes them when it places the program. Can't move once loaded.
Execution Time
dynamic — via MMU
Binding happens on every memory access, in hardware. This is what enables swapping, relocation and paging — the modern default.
🔑
Why Execution-Time Binding Wins

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.

Section 03 · Diagram

Logical vs Physical — Meet the MMU

CPU logical addr 500 MMU limit = 1000 if logical < limit ✓ physical = logical + relocation (14000) RAM physical addr 14500 logical ≥ limit → trap (segfault)
🧮
Two Registers Do It All

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.

Section 04

Swapping — RAM ↔ Backing Store

RAM (fast, small) Process A · 100 MB Backing Store (swap) Process B · 100 MB swap out A → ← swap in B 50 MB/s · 100 MB = 2 s each way
⏱️
Swapping Is Slow — Do the Math

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 s4 s total, plus disk seek/rotation. That cost is why modern systems prefer paging small pieces over swapping whole processes.

Section 05

Contiguous Memory Allocation

OS Kernel0 Process 1base 300 · limit 450300 free · 150750 Process 2base 900 · limit 600900 Process 3 · b1500 l15015001650 physical memory · 1650 KB
📚
One Process, One Contiguous Block

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.

Section 06

First-Fit · Best-Fit · Worst-Fit

🥇
First-Fit
first hole that fits
Scan from the start; take the first hole big enough. Fastest, and works well in practice.
🎯
Best-Fit
smallest hole that fits
Search all holes; take the smallest one that's big enough. Wastes least per allocation, but slower and leaves tiny slivers.
🐢
Worst-Fit
largest hole
Always take the largest hole, hoping the leftover stays usable. Usually the worst — it chews up big holes fast.
🧩
Same Holes, Different Choices

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.

Numerical 1

Where Does the 315 KB Request Go?

Holes (address order): 200, 600, 300, 400, 100 KB. First request: 315 KB.

First-Fit 200 315→here 600 300 400 100 Best-Fit 200 600 300 315→here 400 100 Worst-Fit 200 315→here 600 300 400 100
👀
Three Different Homes for the Same Request

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.

Numerical 1 · Result

Full Run — Requests 315, 195, 450 KB

RequestFirst-FitBest-FitWorst-Fit
315hole 600 → 285 lefthole 400 → 85 lefthole 600 → 285 left
195hole 200 → 5 lefthole 200 → 5 lefthole 400 → 205 left
450largest 400 → FAILShole 600 → 150 leftlargest 300 → FAILS
Served2 of 33 of 3 ✅2 of 3
🏆
Best-Fit Wins This Round

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.

Section 08

Fragmentation — The Silent Memory Killer

📦
Internal
wasted inside a block
A process gets a block slightly bigger than it needs (rounding up), and the leftover inside the block is wasted.
🕳️
External
scattered free holes
Plenty of free memory exists, but split into many small holes — no single hole is big enough for the next process.
The 50% Rule
rule of thumb
With first-fit, for every N blocks allocated, about N/2 blocks' worth is lost to external fragmentation.
🅿️
Back to the Parking Lot

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.

Section 09 · Diagram

Compaction — Make One Big Hole

Before · 4 scattered holes P1 200 100 P2 150 150 P3 200 100 P4 150 100 compact After · one 450 KB hole P1 200 P2 150 P3 200 P4 150 FREE 450 400 fits!
🧹
Slide Everything Down, Merge the Holes

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.

Numerical 2

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).

QuestionWorkingAnswer
Total external fragmentation50 + 30 + 80 + 40200 KB
Fit a 100 KB request now?largest free hole = 80No (80 < 100)
Fit a 200 KB request now?largest free hole = 80No
After compaction?200 KB coalesced into one holeBoth fit ✓
Memory used100 + 200 + 150 + 100 = 550550 KB (73.3%)
💡
200 KB Free — Yet Nothing Fits

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.

Section 12

Seven Rules for Memory Management

🧠 MEMORY MANAGEMENT · CHECKLIST
1
Execution-time binding via the MMU is what makes swapping, relocation and paging possible.
2
Every access checks the limit register then adds the relocation register — logical → physical.
3
Swapping frees RAM but is slow (size ÷ transfer rate, both ways) — prefer paging small pieces.
4
First-Fit is fast, Best-Fit wastes least per request, Worst-Fit is usually the poorest.
5
Internal fragmentation wastes space inside a block; external scatters free space into unusable holes.
6
Compaction merges holes into one big region — powerful but expensive, and needs run-time binding.
7
Total free space can be large yet unusable if fragmented — shape matters as much as size.
FINAL

Sharing One RAM Among Many

3Binding stages
base+limitMMU registers
Best-FitWon Numerical 1
200 KBFragmented & unusable
50%Rule of thumb loss
🎯
You Now Understand Contiguous Memory

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.

📚
Where To Go Next

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