Skip to content
Chethan Yadav_
Operating Systems

OS/Lesson 7 of 9/6 min read

Memory Management

Logical vs physical addresses, MMU, contiguous allocation, fragmentation, paging, page tables, TLB, multilevel page tables, and segmentation.


On this page

Why memory management

Many processes share main memory (RAM). The OS must:

  • Keep track of which parts of memory are used and by whom.
  • Allocate and free memory for processes.
  • Protect processes from reading or writing each other's memory.
  • Make good use of limited RAM.

Logical Address vs Physical Address

  • Logical address (virtual address): generated by the CPU while running a program. Each process sees its own address space starting at 0.
  • Physical address: the actual location in RAM.

The Memory Management Unit (MMU) is hardware that translates logical addresses to physical ones on every memory access. In the simplest scheme it uses a relocation (base) register and a limit register:

if (logical >= limit) trap: segmentation fault
physical = base + logical

Address binding

Binding of instructions and data to addresses can happen at compile time, load time, or execution time. Modern systems bind at execution time, which is what lets processes move in memory and makes virtual memory possible.

Swapping

Swapping temporarily moves a whole process from RAM to disk (the backing store) and later brings it back. It lets the total memory of all processes exceed physical RAM, at the cost of slow disk I/O. Modern systems swap individual pages instead of whole processes (see virtual memory).

Contiguous Allocation

Each process occupies one continuous block of memory.

  • Fixed partitioning: memory split into fixed size partitions. Simple, but wastes space inside partitions.
  • Variable (dynamic) partitioning: allocate exactly what the process needs from a free hole.

Choosing a hole:

StrategyRuleNotes
First fitFirst hole that is big enoughFast, generally good
Best fitSmallest hole that is big enoughLeaves tiny useless holes
Worst fitLargest holeLeaves big holes, usually worst
Next fitFirst fit, but start from last positionSpreads allocations

Fragmentation

Internal Fragmentation

Wasted space inside an allocated block, because the block is bigger than requested. Example: a 4 KB page holding 1 KB of data wastes 3 KB. Happens with fixed partitions and paging.

External Fragmentation

Enough total free memory exists, but it is split into small non-contiguous holes, so a large request fails. Happens with variable partitioning and segmentation.

Compaction

Compaction shuffles allocated blocks together to create one big free hole. Only possible with execution-time binding and it is expensive. Paging avoids the need entirely.

Paging

Paging removes external fragmentation by allowing a process's physical memory to be non-contiguous.

  • Physical memory is divided into fixed size blocks called frames.
  • Logical memory is divided into blocks of the same size called pages (typically 4 KB).
  • Any page can go into any free frame.

A logical address is split into a page number p and a page offset d:

logical address (m bits) = | p (m - n bits) | d (n bits) |    page size = 2^n
physical address          = frame_number(p) * page_size + d

Example: 32-bit address, 4 KB pages (2^12): 20 bits page number, 12 bits offset, so up to 2^20 pages per process.

Paging causes no external fragmentation, but on average half a page of internal fragmentation per process.

Page Table

The page table maps each page number to a frame number. There is one page table per process; the CPU's page table base register (PTBR) points at the current one, which is why a context switch changes it.

Each page table entry (PTE) contains:

  • Frame number
  • Valid/invalid bit (is the page in memory and part of the address space)
  • Protection bits (read, write, execute)
  • Dirty (modified) bit
  • Referenced (accessed) bit

Problem: with the page table in RAM, every memory access needs two memory accesses (one for the PTE, one for the data).

TLB

The Translation Lookaside Buffer (TLB) is a small, fast, associative cache inside the MMU that stores recent page-to-frame translations.

  • TLB hit: frame found immediately.
  • TLB miss: walk the page table in memory, then put the entry into the TLB.

Effective Access Time (EAT), with TLB lookup time t, memory access m, hit ratio h:

EAT = h * (t + m) + (1 - h) * (t + 2m)

Example: t = 20 ns, m = 100 ns, h = 0.8: EAT = 0.8 * 120 + 0.2 * 220 = 96 + 44 = 140 ns.

On a context switch the TLB must be flushed, unless entries are tagged with an ASID (address space identifier). Locality of reference keeps TLB hit ratios above 99% in practice.

Multilevel Page Table

A flat page table for a 32-bit space with 4 KB pages has 2^20 entries of 4 bytes = 4 MB per process, mostly unused. A multilevel (hierarchical) page table pages the page table itself:

| p1 (10 bits) | p2 (10 bits) | d (12 bits) |
p1 indexes the outer table -> gives an inner page table
p2 indexes the inner table -> gives the frame

Only inner tables for used regions are allocated. x86-64 uses 4 levels (and 5 on newer CPUs). Cost: more memory accesses on a TLB miss.

Inverted Page Table

An inverted page table has one entry per physical frame (not per page), storing which process and page occupy it. Saves memory on huge address spaces, but lookup needs a search, usually sped up with a hash table. Used on some PowerPC and IA-64 systems.

Segmentation

Segmentation divides memory into variable size logical units that match the programmer's view: code, stack, heap, globals.

  • Logical address = (segment number, offset).
  • Segment table entries hold base and limit.
  • Supports sharing and protection per logical unit.
  • Suffers from external fragmentation.

Paging vs Segmentation

PagingSegmentation
Fixed size pagesVariable size segments
Invisible to the programmerMatches the program's logical structure
Internal fragmentationExternal fragmentation
Page tableSegment table (base, limit)

Segmented paging combines them: segments are divided into pages. Intel x86 historically used this; x86-64 mostly ignores segmentation.

Quick revision

  • MMU translates logical to physical addresses at runtime.
  • Paging: fixed size pages into frames, no external fragmentation.
  • TLB caches translations; EAT = h(t + m) + (1 - h)(t + 2m).
  • Multilevel page tables save memory for sparse address spaces.
  • Segmentation matches program structure but fragments externally.