Skills101

Operating Systems · Lesson 6 of 9 · Updated ·

Memory Management — Fragmentation, Paging and Segmentation

RAM is limited and shared by many processes. Memory management decides where each process lives in memory and keeps processes from touching each other's memory.

1. The memory hierarchy

Memory hierarchy from fastest and smallest to slowest and biggest: registers, cache, RAM, SSD, hard disk

The CPU is far faster than RAM, and RAM far faster than disk. Caches and the OS keep the data you're using now in the faster levels.

2. Logical vs physical addresses

  • A program uses logical (virtual) addresses — it thinks it starts at address 0.
  • The hardware MMU (Memory Management Unit) translates them to physical addresses in RAM.

This lets many programs each believe they own memory starting at 0, safely.

3. Contiguous allocation and fragmentation

Early systems gave each process one continuous block. To choose a free hole:

StrategyPicksNote
First fitthe first hole big enoughfast
Best fitthe smallest hole big enoughleaves tiny useless holes
Worst fitthe largest holeleaves big holes, rarely best

This causes fragmentation:

External fragmentationInternal fragmentation
Whatenough free memory in total, but split into small non-adjacent holesallocated block is bigger than needed; the extra inside is wasted
Example3 free holes of 10 KB can't fit a 25 KB processasking for 13 KB, getting a 16 KB block → 3 KB wasted
Fixcompaction, or pagingsmaller blocks/pages

4. Paging

Logical pages 0 to 3 are mapped through a page table to physical frames 5, 2, 7 and 0 — pages can sit anywhere in RAM

  • Logical memory is split into fixed-size pages (e.g. 4 KB); physical memory into frames of the same size.
  • A page table maps each page to a frame. Pages don't have to be next to each other → no external fragmentation (only a little internal fragmentation in the last page).

Address translation

A logical address = (page number, offset).

Example: page size 1 KB (1024 bytes). Logical address 2100 → page = 2100 ÷ 1024 = 2, offset = 2100 − 2048 = 52. If page 2 is in frame 7, the physical address = 7 × 1024 + 52 = 7220.

TLB: making paging fast

Looking up the page table in RAM for every access would double memory time. The TLB (Translation Lookaside Buffer) is a tiny, very fast cache of recent page → frame translations. Most lookups hit the TLB.

Effective access time = hit ratio × (TLB time + memory time) + miss ratio × (TLB time + 2 × memory time).

5. Segmentation

Instead of equal pages, memory is split into logical segments of different sizes: code, data, stack, heap. Each segment has a base and a limit.

PagingSegmentation
Unit sizefixed (pages)variable (segments)
Based onhardware conveniencethe program's logical structure
Fragmentationinternal (small)external
Seen by programmer?noyes

Modern systems mostly use paging (sometimes combined with segmentation).

Common mistakes

  • Mixing up internal (inside a block) and external (between blocks) fragmentation.
  • Forgetting that paging removes external fragmentation but not internal.
  • Calculating offset incorrectly — offset = address mod page size.

Quick recap

  • Logical addresses are translated to physical addresses by the MMU.
  • First/best/worst fit for contiguous allocation; external vs internal fragmentation.
  • Paging: fixed pages → frames via a page table; TLB caches translations.
  • Segmentation: variable logical segments with base and limit.

Practice what you learned

📝 Quiz: Memory Management5 questions · instant answers & explanations · +10 XP · counts towards your free certificateTake the quiz →