Memory Management
Memory management is one of the most critical subsystems of an operating system. It handles the allocation, tracking, and回收 of primary memory (RAM), ensuring efficient utilization while providing process isolation and protection.
Why Memory Management Matters
Every program needs memory to store instructions, data, and stack frames. The OS must:
- Allocate memory to processes when needed
- Protect each process’s memory from others
- Share memory efficiently when appropriate
- Reclaim memory when processes terminate
- Virtualize limited physical memory across many processes
Memory Hierarchy
graph TD
A[CPU Registers] -->|~1 ns| B[L1 Cache]
B -->|~2-4 ns| C[L2 Cache]
C -->|~5-12 ns| D[L3 Cache]
D -->|~50-100 ns| E[Main Memory - RAM]
E -->|~5-10 ms| F[SSD / NVMe]
F -->|~5-10 ms| G[HDD / Disk]
style A fill:#ff6b6b,color:#fff
style B fill:#ffa94d,color:#fff
style C fill:#ffd43b,color:#000
style D fill:#69db7c,color:#000
style E fill:#4dabf7,color:#fff
style F fill:#9775fa,color:#fff
style G fill:#868e96,color:#fff
| Level | Size | Latency | Managed By |
|---|---|---|---|
| Registers | ~1 KB | <1 ns | Compiler/CPU |
| L1 Cache | 32-64 KB | ~1 ns | Hardware |
| L2 Cache | 256 KB-1 MB | ~4 ns | Hardware |
| L3 Cache | 4-64 MB | ~12 ns | Hardware |
| RAM | 4-512 GB | ~100 ns | OS + Hardware |
| SSD | 256 GB-4 TB | ~100 μs | OS + Firmware |
| HDD | 1-20 TB | ~5-10 ms | OS + Firmware |
Address Translation
The fundamental challenge: programs use logical (virtual) addresses, but hardware needs physical addresses. The Memory Management Unit (MMU) translates between them.
graph LR
A[CPU] -->|Virtual Address| B[MMU]
B -->|Physical Address| C[Physical Memory]
B -.->|TLB Hit| D[TLB Cache]
D -.->|Fast Lookup| B
style A fill:#4dabf7,color:#fff
style B fill:#ff6b6b,color:#fff
style C fill:#69db7c,color:#000
style D fill:#ffa94d,color:#fff
Key Concepts Across This Section
Allocation Strategies
- Contiguous Allocation — Simplest approach; processes get consecutive physical blocks
- Paging — Fixed-size blocks; eliminates external fragmentation
- Segmentation — Variable-size segments matching program structure
Page Table Management
- Page Tables — Core data structures for address translation
- TLB — Hardware cache for page table entries
- Multi-Level Page Tables — Hierarchical tables to save space
- Inverted Page Tables — One entry per physical frame
Advanced Techniques
- Huge Pages — Larger page sizes for reduced TLB misses
- Swapping — Moving pages to/from disk
- mmap — Memory-mapped files and anonymous mappings
- NUMA — Non-Uniform Memory Access architectures
Allocator Implementations
- Allocation Algorithms — First-fit, best-fit, worst-fit
- Buddy System — Power-of-2 splitting/merging allocator
- Slab Allocator — Kernel object caching
Linux Memory Architecture
graph TB
subgraph "User Space"
A[Process A - Virtual Memory]
B[Process B - Virtual Memory]
C[Process C - Virtual Memory]
end
subgraph "Kernel Space"
D[Virtual Memory Manager]
E[Page Frame Allocator]
F[Slab Allocator]
G[Buddy System]
H[Swap Manager]
end
subgraph "Hardware"
I[MMU + TLB]
J[Physical RAM - Page Frames]
K[Swap Space - Disk]
end
A --> D
B --> D
C --> D
D --> E
D --> F
D --> H
E --> G
F --> G
G --> J
H --> K
I -.-> D
I -.-> J
style A fill:#4dabf7,color:#fff
style B fill:#4dabf7,color:#fff
style C fill:#4dabf7,color:#fff
style D fill:#ff6b6b,color:#fff
style J fill:#69db7c,color:#000
style K fill:#868e96,color:#fff
Quick Reference: Key Terms
| Term | Definition |
|---|---|
| Frame | Fixed-size physical memory block |
| Page | Fixed-size virtual memory block |
| Page Fault | Accessing a page not in physical memory |
| TLB | Translation Lookaside Buffer (page table cache) |
| MMU | Memory Management Unit (hardware translator) |
| Working Set | Set of pages a process actively uses |
| Thrashing | Excessive page faults degrading performance |
| COW | Copy-on-Write — defer copying until modification |
| NUMA | Non-Uniform Memory Access architecture |
| OOM | Out of Memory — kernel kills processes |
Interview Focus Areas
- Paging vs Segmentation — trade-offs, why paging won
- Page fault handling — step-by-step from trap to return
- TLB — what happens on miss, TLB reach
- Thrashing — causes, detection, solutions
- Virtual to physical translation — walk through the hardware
- Linux
/proc/meminfo— understanding each field - malloc vs mmap — when the kernel uses each
- Copy-on-Write — fork() optimization mechanics
Study Path
graph LR
A[Contiguous] --> B[Paging]
B --> C[Page Tables]
C --> D[Multi-Level PT]
B --> E[Segmentation]
C --> F[TLB]
D --> G[Huge Pages]
B --> H[Demand Paging]
H --> I[Page Replacement]
H --> J[Thrashing]
B --> K[mmap]
B --> L[Swapping]
style A fill:#4dabf7,color:#fff
style B fill:#ff6b6b,color:#fff
style H fill:#ffa94d,color:#fff
Start with contiguous allocation (simplest), then paging (modern standard), then build up to advanced topics.