Memory Management
Address binding, contiguous allocation, fragmentation, segmentation vs paging, and how malloc and the kernel allocator manage memory.
The OS must share limited physical memory among many processes, keep them isolated, and make allocation fast. This lesson covers the core ideas and how they led to paging, which every modern OS uses.
Logical vs physical addresses
Programs use logical (virtual) addresses; the hardware MMU translates them to physical addresses at runtime. That indirection lets the OS place a process anywhere in RAM, move it, and protect it.
Historically, binding happened at different times:
| When | How | Flexibility |
|---|---|---|
| Compile time | Absolute addresses hard-coded | The program must load at a fixed place |
| Load time | The loader patches addresses (relocation) | Can't move once loaded |
| Run time | The MMU translates every access | Can move, swap and share — the modern approach |
Contiguous allocation and fragmentation
Early systems gave each process one contiguous block, protected by base and limit registers. Allocating variable-sized blocks leaves gaps:
- External fragmentation — there's enough free memory in total, but not in one contiguous piece. (4 free units above, but no single gap larger than 2.)
- Internal fragmentation — memory allocated but unused within a block, because sizes are rounded up.
Placement strategies:
| Strategy | Pick | Note |
|---|---|---|
| First fit | The first hole big enough | Fast; fine in practice |
| Best fit | The smallest hole big enough | Leaves tiny useless slivers |
| Worst fit | The largest hole | Generally worst |
Compaction (sliding processes together) fixes external fragmentation but is expensive. The real fix is to stop requiring contiguity.
Segmentation
Divide a program into logical segments — code, data, stack, heap — each with its own base and limit. It matches how programmers think and allows per-segment protection, but segments are still variable-sized, so external fragmentation remains. x86 still has segment registers, but 64-bit mode essentially ignores them (except FS/GS for thread-local storage).
Paging
Split virtual memory into fixed-size pages and physical memory into equal-sized frames (usually 4 KB). Any page can go in any frame.
- No external fragmentation: any free frame works.
- Internal fragmentation is at most one partial page per region.
- Pages can be shared (libraries), marked read-only or no-execute, loaded lazily, and swapped to disk.
The details — page tables, TLBs, page faults and demand paging — are in Paging & virtual memory.
Allocators inside the kernel
The kernel needs physical memory for itself too:
- Buddy allocator — manages free frames in power-of-two blocks. A request splits a larger block in halves ("buddies") until it fits; freed buddies merge back. Fast and limits external fragmentation, at the cost of internal fragmentation.
- Slab allocator — caches of pre-initialised, fixed-size objects (inodes, task structs, sockets) carved out of pages. Allocation is very fast and there's no per-object fragmentation.
User-space allocators: malloc and friends
malloc/free (and new in C++) manage the heap within a process. They get big chunks from the kernel (brk/sbrk, or mmap for large allocations) and carve them up:
- Free lists and size classes (bins) to find blocks quickly.
- Per-thread caches to avoid lock contention (tcmalloc, jemalloc, mimalloc).
- Coalescing adjacent free blocks to fight fragmentation.
Garbage-collected runtimes (JVM, V8, Go) do their own heap management with bump-pointer allocation and compaction. See Garbage collection algorithms.
Overcommit and the OOM killer
Linux lets processes allocate more virtual memory than exists, because most allocations are never fully touched. If physical memory and swap really run out, the OOM killer picks a process (usually the biggest, adjusted by oom_score_adj) and kills it with SIGKILL. In containers, exceeding the cgroup memory limit triggers the same thing: the dreaded OOMKilled status in Kubernetes.
- The MMU translates logical to physical addresses at runtime, enabling relocation and protection.
- Contiguous allocation suffers external fragmentation; rounding causes internal fragmentation.
- Segmentation is logical but variable-sized; paging uses fixed-size pages and eliminates external fragmentation.
- The kernel uses buddy and slab allocators; malloc uses bins and per-thread caches.
- Linux overcommits memory; the OOM killer resolves real exhaustion.
QInternal vs external fragmentation?easy
Internal: wasted space inside an allocated block because the block is larger than requested (rounded up to a page or size class). External: free memory split into pieces too small to satisfy a request even though the total would be enough. Paging eliminates external fragmentation at the cost of some internal fragmentation.