Skip to content
CurriculumLesson 15 ยท Memory
Intermediate~14 min

Contiguous Allocation & Fragmentation

The simplest memory manager gives each process one contiguous block. It is fast and easy to explain, but free memory slowly turns into a patchwork of holes.

In plain English

Contiguous memory is one long row of seats for a group. If the group grows, it may not fit.

By the end you will be able to

  • Explain contiguous allocation and the free list.
  • Trace first-fit, best-fit, and worst-fit placement.
  • Distinguish internal and external fragmentation.
  • Know when compaction is useful.
01

The free list

Memory is divided into a free list of holes and an allocated region for each process. When a process arrives, the allocator chooses a hole large enough and splits it. When a process exits, its block returns to the free list and may be merged with adjacent free blocks. The policy for choosing a hole changes fragmentation and speed.

02

First-fit, best-fit, worst-fit

First-fit takes the first hole that fits. Best-fit takes the smallest hole that fits, hoping to preserve large holes. Worst-fit takes the largest hole, hoping to leave a usable remainder. None is universally best. First-fit is usually fastest; best-fit can create tiny unusable leftovers; worst-fit spreads allocations out.

lesson-15.osOS
holes = [100K, 500K, 200K, 300K, 600K]

first_fit(212K) -> 500K hole, remainder 288K
best_fit(212K)  -> 300K hole, remainder 88K
worst_fit(212K) -> 600K hole, remainder 388K
03

Fragmentation

External fragmentation means there is enough total free memory but no single hole large enough. Compaction can move processes together to create a large hole, but it costs time and may require relocation support. Internal fragmentation means a process was given more space than it needs, wasting the difference inside its own block. Fixed-size allocation units cause internal fragmentation; variable contiguous blocks cause external fragmentation.

Key takeaways

  • Contiguous allocation gives each process one block.
  • First-fit is fast; best-fit preserves large holes but can leave tiny remnants.
  • External fragmentation is free space split into unusable holes.
  • Internal fragmentation is wasted space inside an allocated block.
  • Compaction fixes external fragmentation at a cost.

Common pitfalls

  • Confusing internal and external fragmentation.
  • Assuming best-fit always improves memory utilization.
  • Forgetting to merge adjacent free blocks on release.

Kernel notes

  • Run first-fit and best-fit on a small hole list.
  • Explain why a request can fail despite enough total free memory.
  • Know the cost of compaction.

Quick check

0 / 2 answered

1. A request fails although total free memory is sufficient. What is this called?

2. Which placement policy chooses the smallest hole that fits?