Memory Management
Memory management controls how physical memory is allocated, tracked, and protected. This is foundational knowledge for understanding virtual memory and system performance.Interview Frequency: High
Key Topics: Allocation strategies, fragmentation, memory protection, buddy system
Time to Master: 10-12 hours
Key Topics: Allocation strategies, fragmentation, memory protection, buddy system
Time to Master: 10-12 hours
Memory Hierarchy
Fast memory is expensive and scarce; cheap memory is slow and abundant. The OS manages this hierarchy to give the illusion of large, fast memory.Numbers Everyone Should Know
Understanding the relative latencies is crucial for performance tuning:| Component | Latency (Time) | Latency (Scaled to Human Scale) |
|---|---|---|
| L1 Cache | 1 ns | 1 second |
| L2 Cache | 4 ns | 4 seconds |
| Main Memory (RAM) | 100 ns | ~2 minutes |
| SSD I/O | 16,000 ns (16 μs) | ~4.5 hours |
| Network (Data Center) | 500,000 ns (0.5 ms) | ~6 days |
| Disk I/O (HDD) | 4,000,000 ns (4 ms) | ~1.5 months |
| Ping (Internet) | 150,000,000 ns (150 ms) | ~5 years |
Address Binding
Programs deal with logical addresses (virtual). The hardware deals with physical addresses. Binding is the mapping process.When are addresses determined?
| Binding Time | Description | Example |
|---|---|---|
| Compile time | Fixed physical address. | Embedded systems, Bootloaders. If process moves, must recompile. |
| Load time | Address determined when loaded. | Relocatable code. Compiler generates offsets; Loader adds base address. |
| Run time | Address changes during execution. | Modern OS. Needs hardware support (MMU/Base-Limit). |
Address Translation Example (Base + Limit)
Simple hardware protection using two registers: Base (start of physical) and Limit (size).Contiguous Memory Allocation
Fixed Partitioning
Variable Partitioning
Allocation Strategies
First Fit
Cons: Fragments front of memory
Best Fit
Cons: Slow (searches all), creates tiny unusable holes
Worst Fit
Cons: Fragments large holes quickly
Comparison
| Strategy | Speed | Fragmentation | Use Case |
|---|---|---|---|
| First Fit | Fast | Moderate | General purpose |
| Best Fit | Slow | Many tiny holes | When memory tight |
| Worst Fit | Slow | Fragments large holes | Rarely used |
| Next Fit | Fast | Spreads fragmentation | Circular allocation |
Fragmentation
Internal Fragmentation
Memory wasted inside allocated blocks:External Fragmentation
Free memory between allocated blocks:Solutions to External Fragmentation
-
Compaction: Move processes to consolidate free space
- Expensive, requires relocation support
-
Paging: Non-contiguous allocation (covered in virtual memory)
- Most common modern solution
-
Segmentation: Variable-sized logical units
- Still has external fragmentation
Buddy System Allocator
Used in Linux kernel for page allocation. It mitigates external fragmentation by merging adjacent free blocks (“buddies”).How It Works
Memory is conceptually a large power-of-2 block.- Splitting: If a request is size , find the smallest chunk size . If the chunk is too big, split it in two equal buddies. Repeat until specific size matches.
- Coalescing: When freeing a block, check if its buddy is free. If yes, merge them into a larger block. Repeat up the chain.
Example Walkthrough (1MB Total Memory)
Process A requests 70KB. Smallest power-of-2 fitting 70KB is 128KB.- Initial: One 1MB free block.
- Split 1MB -> 512KB + 512KB
- Split 512KB -> 256KB + 256KB
- Split 256KB -> 128KB + 128KB
- Allocate: First 128KB is given to Process A.
- Check buddy (next 128KB) -> It’s FREE. Merge -> 256KB.
- Check buddy (next 256KB) -> It’s FREE. Merge -> 512KB.
- Check buddy (next 512KB) -> It’s FREE. Merge -> 1MB. Result: Back to original 1MB contiguous block.
Implementation
Buddy System Properties
| Property | Value |
|---|---|
| Allocation sizes | Powers of 2 only |
| Internal fragmentation | Up to ~50% |
| External fragmentation | Low (coalescing) |
| Speed | O(log n) |
| Used by | Linux kernel page allocator |
Slab Allocator
For frequently allocated fixed-size objects:SLUB (Modern Linux)
Memory Protection
Base and Limit Registers
Segmentation with Protection
Page-Level Protection (Modern)
Swapping
Moving entire processes to/from disk:Modern: Demand Paging vs Swapping
| Aspect | Swapping | Demand Paging |
|---|---|---|
| Granularity | Entire process | Single pages |
| Latency | Very high | Lower |
| Flexibility | Coarse | Fine-grained |
| Used by | Older systems | Modern OS |
Memory-Mapped Files
Map file contents directly to memory:Use Cases
- Shared libraries:
.sofiles mapped read-only - Large files: Database files, video editing
- IPC: Multiple processes map same file
- Copy-on-write: Fork efficiency
IOMMU and DMA Remapping
Modern systems provide hardware-assisted memory protection for DMA devices:IOMMU Architecture
DMA Remapping Use Cases
Linux IOMMU API
IOMMU Performance Considerations
Interview Deep Dive Questions
Q1: Explain internal vs external fragmentation
Q1: Explain internal vs external fragmentation
Answer:Internal Fragmentation:External Fragmentation:Solutions:
- Memory wasted inside an allocated block
- Occurs with fixed-size allocation
- Example: Allocating 128 bytes for a 100-byte request
- Free memory between allocated blocks
- Total free may be enough, but not contiguous
- Occurs with variable-size allocation
- Internal: Better size classes, slab allocator
- External: Compaction, paging (most effective)
Q2: How does the buddy system prevent external fragmentation?
Q2: How does the buddy system prevent external fragmentation?
Answer:The buddy system uses coalescing to prevent fragmentation:Key Mechanisms:Tradeoff: Up to 50% internal fragmentation for small requests
-
Power-of-2 sizes only:
- All blocks are 2^n bytes
- When split, creates two equal “buddies”
-
Buddy address calculation:
-
Coalescing on free:
- When freeing, check if buddy is also free
- If yes, merge into larger block
- Repeat until buddy not free or max size
Q3: Why does Linux use both buddy system and slab allocator?
Q3: Why does Linux use both buddy system and slab allocator?
Answer:Different allocation patterns need different strategies:Buddy System (Page Allocator):Examples:
- Allocates pages (4KB units)
- Good for: DMA buffers, large allocations
- Issues: Internal fragmentation for small objects
- Used by:
alloc_pages(),get_free_pages()
- Allocates objects of fixed sizes
- Good for: Frequently allocated kernel structures
- Benefits: No fragmentation, object caching
- Used by:
kmalloc(),kmem_cache_alloc()
struct task_struct: Slab cache (same size, frequent)kmalloc(4096): Could use buddy directly- Video buffer: Buddy system for contiguous pages
Q4: What happens during an out-of-memory (OOM) situation?
Q4: What happens during an out-of-memory (OOM) situation?
Answer:Linux OOM Handling:Common Causes:
-
Memory Pressure Detection:
- Buddy system can’t find pages
kswapddaemon activated
-
Reclaim Attempts:
-
OOM Killer Activated (if reclaim fails):
-
Victim Selection:
- Highest
oom_scorewins - Process receives
SIGKILL - Kernel frees its memory
- Highest
- Memory leak in application
- Insufficient swap
- Overcommit settings too aggressive
- Fork bomb
Q5: Explain memory overcommit in Linux
Q5: Explain memory overcommit in Linux
Answer:Overcommit allows allocating more virtual memory than physical RAM + swap.Why?
Example:View current state:Tradeoffs:
- Fork creates complete copy of address space
- Most pages never modified (COW)
- Sparse arrays allocate but don’t use
/proc/sys/vm/overcommit_memory):| Value | Mode | Behavior |
|---|---|---|
| 0 | Heuristic | Kernel estimates, rejects “obvious” overcommit |
| 1 | Always | Never fail malloc (dangerous!) |
| 2 | Never | Strict: CommitLimit = RAM + Swap * ratio |
- Overcommit (mode 0/1): More flexible, risk of OOM
- Strict (mode 2): Safe for databases, may waste resources
- Databases: Mode 2 (predictable)
- Development: Mode 0 (default)
- Never in production: Mode 1
Practice Exercises
1
Implement First-Fit Allocator
Build a simple memory allocator with first-fit strategy and coalescing.
2
Buddy System Simulation
Simulate buddy allocation and visualize the splitting/merging process.
3
Fragmentation Analysis
Write a program to measure internal vs external fragmentation under different workloads.
4
OOM Score Calculator
Implement Linux’s OOM scoring algorithm.
Key Takeaways
Fragmentation Types
Internal (inside blocks) vs External (between blocks). Paging solves external.
Buddy System
Power-of-2 allocation with coalescing. Used for kernel page allocation.
Slab Allocator
Fixed-size object caching. Zero fragmentation for same-size objects.
Protection
Base/limit, segmentation, paging all provide memory isolation.
Next: Inter-Process Communication →