Memory Management (mm/)
How Linux manages the most fundamental resource: memory
Getting Started
This documentation explains how Linux manages memory - not just the theory, but the actual implementation decisions, trade-offs, and lessons learned over 30+ years of development.
Prerequisites
This documentation assumes familiarity with: - C programming - The kernel is written in C - Pointers and memory addresses - Virtual vs physical addressing - Basic OS concepts - Processes, kernel vs userspace
If you've covered virtual memory and paging, that's a good foundation. See Further Reading for recommended textbooks.
Getting the Kernel Source
For building and configuration, see Documentation/admin-guide/README.rst.
Running Tests
The fastest way to test kernel changes without rebooting your machine:
# Install virtme-ng (runs kernel in QEMU with your filesystem)
pip install virtme-ng
# Build and boot with a test module
virtme-ng --kdir . --append 'test_vmalloc.run_test_mask=0xFFFF'
# Check results
# (dmesg output appears in the virtual console)
For comprehensive testing documentation, see Documentation/dev-tools/testing-overview.rst.
Suggested Reading Order
Fundamentals:
- Overview - The narrative of how mm/ evolved
- Page Allocator - Buddy system fundamentals
- Slab - SLUB allocator for small objects
- vmalloc - Virtual memory allocation
Address Translation & Process Memory:
- Page Tables - Virtual address translation
- Process Address Space - VMAs, mmap, demand paging
Memory Pressure & Reclaim:
- Page Reclaim - LRU, kswapd, MGLRU
- Swap - Extending memory to disk
- Page Cache - File data caching
Advanced Topics:
- Memory Cgroups - Container memory limits
- NUMA - Multi-node memory management
- Transparent Huge Pages - Automatic huge pages
- Compaction - Memory defragmentation
- KSM - Page deduplication for VMs
What You'll Learn
| Textbook Concept | Linux Reality |
|---|---|
| "Page tables map virtual to physical" | Multi-level page tables, TLB flushing costs, lazy unmapping |
| "Memory allocator" | Multiple allocators for different use cases (slab, vmalloc, buddy) |
| "Fragmentation" | Why physically contiguous memory is hard, vmalloc as a solution |
| "Memory is finite" | OOM killer, memory cgroups, reclaim |
What Makes This Different
- Real commits: Every claim links to actual kernel commits
- Real bugs: Learn from what broke and why
- Real discussions: Links to LKML where decisions were debated
- Real trade-offs: Not just "what" but "why this and not that"
- Hands-on: "Try It Yourself" sections with commands to run and code to read
Documentation
Core Reading
| Document | What You'll Learn |
|---|---|
| overview | The 30-year story of Linux memory management |
| page-allocator | Buddy system - physical page management |
| slab | SLUB - small object allocation |
| vmalloc | Virtually contiguous allocation |
| vrealloc | Resizing allocations |
| page-tables | Virtual address translation |
| reclaim | What happens under memory pressure |
| memcg | Container memory limits |
| thp | Transparent Huge Pages - automatic 2MB pages |
| numa | NUMA memory policies and balancing |
| compaction | Memory defragmentation for large allocations |
| ksm | Kernel Same-page Merging - memory deduplication |
| mmap | Process address space and VMAs |
| page-cache | File data caching in memory |
| swap | Extending memory to disk |
Explainers
Conceptual guides for common memory management questions:
| Document | What You'll Learn |
|---|---|
| virtual-physical-resident | VSZ vs RSS vs PSS vs USS - why memory numbers don't add up |
| overcommit | Why overcommit is the only sane default |
| brk-vs-mmap | Two ways to get memory from the kernel |
| contiguous-memory | Why large allocations fail despite free memory |
| cow | Copy-on-write edge cases and when it breaks |
Key Source Files
If you want to read the actual code:
| File | What It Does |
|---|---|
mm/page_alloc.c |
Buddy allocator - physical page management |
mm/slub.c |
SLUB allocator - small object caches |
mm/vmalloc.c |
vmalloc - virtually contiguous allocation |
mm/vmscan.c |
Memory reclaim under pressure |
mm/memcontrol.c |
Memory cgroups |
mm/huge_memory.c |
Transparent Huge Pages |
mm/mempolicy.c |
NUMA memory policies |
mm/compaction.c |
Memory compaction |
mm/ksm.c |
Kernel Same-page Merging |
mm/mmap.c |
Process address space, VMAs |
mm/filemap.c |
Page cache |
mm/swapfile.c |
Swap area management |
include/linux/gfp.h |
GFP flags definitions |
Further Reading
Free Resources
- Bonwick, The Slab Allocator (1994) - Original slab paper from USENIX
- Gorman, Understanding the Linux Virtual Memory Manager - Free online book
Kernel Documentation
- Documentation/mm/ in the kernel tree
- Documentation/admin-guide/mm/ for sysadmin perspective
Textbooks
- Silberschatz et al., Operating System Concepts - Ch. 9-10 (Memory)
- Tanenbaum & Bos, Modern Operating Systems - Ch. 3 (Memory Management)
- Love, Linux Kernel Development - Ch. 12 (Memory Management)
Appendix
- Glossary - Terminology reference