4.2.24 · D3Operating Systems

Worked examples — Fragmentation — internal vs external, compaction

2,347 words11 min readBack to topic

This page is a drill. We take the two formulas from the parent topic and beat them against every kind of input they can face — clean numbers, exact fits, tiny processes, huge processes, the degenerate "one hole" case, a word problem, and an exam trap.

Before we start, three symbols carry the whole page. We define them from zero:

The marks are the ceiling — "round up to the next whole number". We read as: "2.44 blocks doesn't exist, so you need 3 whole blocks." Why up and never down? Because rounding down would leave some of the process's bytes with no home — illegal. Every leftover byte forces a fresh block.


The scenario matrix

Here is every distinct case class fragmentation can throw at you. The examples below each name the cell they cover.

Cell Case class What's special about it
A Internal, non-exact fit not a multiple of → some waste
B Internal, exact fit (degenerate) is a multiple of → waste is zero
C Internal, tiny process () needs 1 block, wastes almost all of it
D Internal, many processes (system total) sum many tails; average each
E External, scattered holes ratio between 0 and 1
F External, one single hole (degenerate) ratio , perfectly healthy
G External, request fails but total suffices the classic "enough but useless" trap
H Compaction outcome holes merge → request now succeeds
I Word problem (real units, KB/MB) translate English → the formulas
J Exam twist paging vs. compaction confusion / limiting behaviour

Example 1 — Cell A (internal, non-exact fit)


Example 2 — Cell B (exact fit, degenerate: zero waste)


Example 3 — Cell C (tiny process, )


Example 4 — Cell D (system total, many processes)


Example 5 — Cell E & G (external: scattered holes, request fails)

Figure — Fragmentation — internal vs external, compaction
  1. Total free: K. Why this step? The ratio compares the biggest usable piece against all the free memory.
  2. Largest block: K (the amber bar in the figure).
  3. Ratio: . Why this step? A ratio near 1 means the free memory is badly shattered; here over half is unusable for any request above 50 K.
  4. The 100 K request: must land in one contiguous hole. Largest is 50 K K → fails, even though . Why this step? Memory handed to a process must be contiguous; you cannot stitch three separate holes together for one request.

Verify: ; and indeed . ✓ Request fails because no single hole K.


Example 6 — Cell F (degenerate external: one hole, ratio zero)


Example 7 — Cell H (compaction merges the holes)

Figure — Fragmentation — internal vs external, compaction
  1. Slide A, B, C to the front: all used blocks pack against one end (top of figure → bottom). Why this step? Compaction physically copies allocated blocks together; this requires run-time relocatable addresses (a base/relocation register), else the copied code would point at the wrong places.
  2. Holes merge: the freed space becomes one block of K. Why this step? Nothing was freed or created — the same 120 K is now contiguous instead of scattered.
  3. New ratio: .
  4. 100 K request: in one block → succeeds. Why this step? Now a single hole is large enough, which is exactly what the request needs.

Verify: total free unchanged ( K before and after — compaction moves, never deletes); ratio dropped . ✓ And , so success. Note internal waste inside A, B, C is untouched — compaction cures external only.


Example 8 — Cell I (word problem, real units)


Example 9 — Cell J (exam twist: which cure, and the limit)


Recall One-line recall of the whole matrix

Internal formula ::: ; zero only when is a multiple of ; worst case ; system total = sum of tails ( each on average). External ratio ::: ; equals 0 for one hole, rises toward 1 as memory shatters; compaction drives it to 0 without changing total free.


Back to the parent topic. Related machinery: Paging, Segmentation, Dynamic Memory Allocation, Memory Allocation Strategies, Virtual Memory, Address Binding.