Skip to content

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

git clone https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
cd linux

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:

  1. Overview - The narrative of how mm/ evolved
  2. Page Allocator - Buddy system fundamentals
  3. Slab - SLUB allocator for small objects
  4. vmalloc - Virtual memory allocation

Address Translation & Process Memory:

  1. Page Tables - Virtual address translation
  2. Process Address Space - VMAs, mmap, demand paging

Memory Pressure & Reclaim:

  1. Page Reclaim - LRU, kswapd, MGLRU
  2. Swap - Extending memory to disk
  3. Page Cache - File data caching

Advanced Topics:

  1. Memory Cgroups - Container memory limits
  2. NUMA - Multi-node memory management
  3. Transparent Huge Pages - Automatic huge pages
  4. Compaction - Memory defragmentation
  5. 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

Kernel Documentation

Textbooks


Appendix