Leanpub Header

Skip to main content

Linux Memory Management: From Fundamentals to Kernel Internals

A Complete Guide to Virtual Memory, Allocators, Paging, and Performance in the Modern Linux Kernel

This book is 100% completeLast updated on 2026-08-01

Memory is at the heart of Linux performance. This book explains how the kernel manages it from virtual memory and page allocation to NUMA, cgroups and real-world optimization. Learn through clear explanations, kernel source examples and practical exercises you can run yourself.

Minimum price

$19.00

$29.00

You pay

Author earns

$

Also available for 1 book credit with a Reader Membership

PDF
EPUB
WEB
APP
179
Pages
About

About

About the Book

This book takes you on a systematic journey through every layer of Linux memory management. You will learn how virtual memory works from silicon up to application code, how the kernel allocates and reclaims physical pages, how allocators optimize for speed and fragmentation, how NUMA systems distribute memory across nodes, how containers isolate resources with cgroups, and how to profile, debug, and harden memory behavior in production. Every concept is explained with clear reasoning, concrete examples from real kernel source code, and hands-on exercises you can run on your own system. The target audience is systems programmers, kernel developers, performance engineers, and advanced software practitioners who need deep understanding of how Linux manages memory.

Author

About the Author

Steve Publications

Steve is a technology professional with more than 20 years of experience in software development, server infrastructure, cybersecurity, vulnerability research and reverse engineering. Throughout his career, he has designed, secured, analyzed and tested complex software and infrastructure, with a particular focus on understanding how systems fail and how they can be made more secure.

Outside of work, Steve enjoys sharing knowledge with the technology community. He collaborates with researchers, industry experts and technology professionals to write practical books covering software development, cybersecurity, cloud computing, networking, DevOps, artificial intelligence and enterprise technologies. His books focus on practical learning through clear explanations, real-world examples and hands-on exercises. With more than two decades of industry experience, his goal is to help IT professionals, students and technology enthusiasts build useful skills and stay current in a rapidly changing industry.

We believe readers deserve to know how our books are created. Most of our authors are not native English speakers, so we use AI to help translate, proofread manuscripts, fix grammar, improve sentence structure and make technical explanations easier to read. AI is used as an editing tool only. It does not replace the research, technical knowledge or hands-on experience behind our books. Some of our authors also prefer to remain anonymous for privacy or professional reasons. In those cases, we publish their work under a different name. The author's name may be different, but the quality of the content and our review process remain the same.

Every book is written, reviewed and maintained by experienced technology professionals, with contributions from our private technical community of more than 420 engineers and researchers from Ukraine, Belarus and Russia. We spend far more time validating technical accuracy and keeping our content up to date than generating text. We are always interested in working with experienced professionals who have deep expertise in a particular technology or domain. If you would like to publish a book with us or help review an existing manuscript, we'd love to hear from you. Send us a message describing your area of expertise. We are especially interested in niche technologies, specialized skills and emerging topics that are underrepresented in existing technical literature.

If you look through the contents of our books, you'll see practical examples, detailed explanations and material that is regularly updated. Our goal is to publish books that professionals can actually rely on, not low-effort AI-generated content. If you ever feel that one of our books does not meet that standard, Leanpub offers a 60-day money-back guarantee. Feel free to request a refund if you are not satisfied with your purchase.

Contents

Table of Contents

A Complete Guide to Virtual Memory, Allocators, Paging, and Performance in the Modern Linux Kernel*

Introduction

Chapter 1: Foundations of Memory Abstraction

  1. The Memory Problem: Why Programs Cannot Share Physical Memory Directly
  2. Virtual Memory as an Abstraction Layer
  3. Processes, Address Spaces, and Isolation
  4. The Role of the Operating System in Memory Management
  5. Hardware Support: MMUs and Protection
  6. The Linux Memory Management Philosophy

Chapter 2: Hardware Foundations and Address Translation

  1. Memory-Mapped I/O and Physical Addressing
  2. The Memory Management Unit Architecture
  3. Page Tables: Structure, Levels, and Walks
  4. Translation Lookaside Buffers and TLB Shootdowns
  5. x86-64 Paging: Four-Level and Five-Level Page Tables
  6. ARM64 and RISC-V Virtual Memory Systems
  7. Memory Types, Caching Attributes, and Access Permissions
  8. Walking a Page Table by Hand: A Practical Example

Chapter 3: Linux Address Spaces and the Virtual Memory Layout

  1. The Process Address Space in Linux
  2. User-Space Layout: Code, Data, Heap, Stack, and Mapped Regions
  3. Kernel Virtual Address Space and Direct Mapping
  4. Architecture-Specific Layouts: x86-64, ARM64, and RISC-V
  5. Address Space Layout Randomization and Security
  6. The mm_struct and vma_struct: Core Data Structures
  7. Memory Region Management with mmap, brk, and mremap
  8. Practical Experiment: Inspecting Your Process’s Memory Map

Chapter 4: Physical Memory Management and the Buddy Allocator

  1. Tracking Physical Pages: The page Struct and Memmap
  2. Memory Zones and Node Architecture
  3. The Buddy Allocator Algorithm and Implementation
  4. Free Page Lists and Order Management
  5. Allocation Paths: GFP Flags and Context Sensitivity
  6. Fragmentation Analysis and Mitigation Strategies
  7. NUMA-Aware Allocation and Policy
  8. Source Walkthrough: mm/page_alloc.c

Chapter 5: Kernel Memory Allocators: Slab, Slub, and SLOB

  1. Why Kernel Needs Specialized Allocators
  2. The Slab Allocator Design and History
  3. Slub: Simplification and Performance Improvements
  4. SLOB: Minimalist Allocation for Embedded Systems
  5. Per-CPU Caches and Lockless Fast Paths
  6. kmalloc, kcalloc, and the Kernel Allocation API
  7. Cache Colocation, False Sharing, and NUMA Effects
  8. Building a Custom Kernel Allocator Module

Chapter 6: User-Space Memory Allocation and vmalloc

  1. The malloc Contract and POSIX Requirements
  2. glibc ptmalloc: Arenas, Bins, and Thread Safety
  3. Alternative Allocators: jemalloc and tcmalloc
  4. mmap vs brk: Strategies for Large and Small Allocations
  5. vmalloc and the Kernel’s Virtual Memory Allocator
  6. vmap, ioremap, and Special Mapping Regions
  7. Performance Comparison: Benchmarking User-Space Allocators
  8. Practical Experiment: Tracing malloc Behavior with eBPF

Chapter 7: Demand Paging, Page Faults, and Copy-on-Write

  1. The Lazy Loading Philosophy: Demand Paging
  2. Page Fault Types and Handling Flow
  3. Major and Minor Faults: Performance Implications
  4. File Backed Pages and the Page Cache Introduction
  5. Copy-on-Write Semantics and Implementation
  6. Fork, Exec, and Memory Efficiency
  7. Shared Memory Regions and mmap SHARED vs PRIVATE
  8. Source Walkthrough: The Page Fault Handler in mm/memory.c

Chapter 8: The Page Cache and Buffer Head Architecture

  1. Why Caching File Data in RAM Transforms Performance
  2. Page Cache Data Structures: Radix Trees and XArrays
  3. Page State Machine and Lifecycle Management
  4. Readahead Algorithms and Prefetching Strategies
  5. Dirty Pages, Writeback, and Flush Daemons
  6. Buffer Heads, Extents, and Historical Evolution
  7. Interaction with Filesystems and Block Devices
  8. Practical Experiment: Measuring Page Cache Hit Rates

Chapter 9: Swapping, Reclaim, and Page Replacement Policies

  1. When RAM Runs Out: The Reclaim Problem
  2. Active and Inactive Page Lists
  3. Page Replacement Policies and LRU Approximation
  4. Swap Spaces, Swap Devices, and File-Based Swapping
  5. The Swap-In and Swap-Out Pathways
  6. zswap, z3fold, and Compressed Caching
  7. Thrashing Detection and Prevention
  8. Practical Experiment: Tuning vm.swappiness and Observing Behavior

Chapter 10: Memory Compaction, Defragmentation, and Huge Pages

  1. Fragmentation in Long-Running Systems
  2. The Compaction Algorithm: Migration and Scanning
  3. Direct Reclaim vs Kswapd vs Compact Daemons
  4. Huge Pages and TLB Efficiency
  5. Transparent Huge Pages: Automatic Promotion and Demotion
  6. hugetlbfs and Preallocated Huge Page Pools
  7. Performance Impact: Benchmarks and Trade-offs
  8. Practical Experiment: Enabling and Measuring THP Effects

Chapter 11: NUMA, Memory Policy, and Scalability

  1. The NUMA Problem: Distance Matters
  2. Node, Zone, and Memory Hierarchy in Linux
  3. Memory Policies: Default, Interleave, Bind, and Prefer
  4. Task Migration and Memory Placement Coordination
  5. Per-NUMA Node Allocators and Caches
  6. Scalability Challenges on Large Systems
  7. Performance Measurement and NUMA-Aware Programming

Chapter 12: Memory Cgroups, Controllers, and Resource Limits

  1. Containerization and the Need for Memory Isolation
  2. Cgroup Hierarchy and Memory Controller Architecture
  3. Memory Limits, Soft Limits, and Thresholds
  4. Accounting: Tracking Usage Per-Cgroup
  5. Reclaim Under Constraints: Hierarchical Pressure
  6. Swap Accounting and Memory+Swap Limits
  7. OOM Behavior Within Cgroups
  8. Practical Experiment: Setting Up Memory-Limited Containers

Chapter 13: Out-of-Memory Handling, Panic, and Recovery

  1. When Reclaim Fails: The Last Resort
  2. The OOM Killer Algorithm and Victim Selection
  3. Scoring Factors and Heuristics
  4. OOM Score Adjustment and Task Protection
  5. System-wide vs Cgroup-local OOM Events
  6. Kernel Panic Conditions and Memory Exhaustion Scenarios
  7. Recovery Strategies and Production Best Practices
  8. Real-World Case Studies: OOM Incidents and Post-Mortems

Chapter 14: Synchronization, Concurrency, and Locking in Memory Management

  1. Concurrency Challenges in a Shared Allocator
  2. Locking Hierarchy and Deadlock Prevention
  3. Page Table Locks and RCU Protection
  4. Zone Locks, PGDAT Locks, and Fine-Grained Contention Control
  5. Per-CPU Data Structures and Lockless Fast Paths
  6. Reference Counting and RCU in Page Lifecycle Management
  7. Memory Barriers, Ordering, and Cache Coherency
  8. Scalability Analysis: Contention on Large Systems

Chapter 15: Performance Optimization, Profiling, and Debugging

  1. Memory Performance Metrics That Matter
  2. Using /proc and /sys for Runtime Inspection
  3. Perf Tools: Flame Graphs and Memory Profiling
  4. eBPF and BCC: Custom Tracing Instruments
  5. ftrace and Kernel Function Tracing
  6. Kernel Module Development for Memory Experiments
  7. Crash Dump Analysis with crash and kdump
  8. Practical Experiment: Building a Complete Debugging Workflow

Chapter 16: Security Implications, Exploits, and Mitigations

  1. Memory Corruption and Kernel Exploitation
  2. Use-After-Free and Slab-Based Attacks
  3. Information Leaks Through Page Reuse
  4. ASLR, KASLR, and Address Randomization
  5. Stack Protection and Canary Values
  6. SMAP, SMEP, and Supervisor Mode Protections
  7. MDS, Spectre, Meltdown, and TLB Side Channels
  8. Kernel Hardening: CONFIG Options and Best Practices

Chapter 17: Architecture-Specific Implementations and Source Walkthroughs

  1. Architecture Abstraction Layers in the Kernel
  2. x86-64 Implementation Details and Optimizations
  3. ARM64 Memory Management and Attribute Indirection
  4. RISC-V Sv39, Sv48, and Sv57 Paging Modes
  5. TLB Shootdown Differences Across Architectures
  6. IOMMU and DMA Mapping Considerations
  7. Navigating the Kernel Source Tree: A Practical Guide
  8. Building and Booting a Custom Kernel for Experimentation

Conclusion: Synthesis and Future Directions

  1. Key Design Principles
  2. Emerging Trends
  3. Final Perspective

References

Get the free sample chapters

Click the buttons to get the free sample in PDF or EPUB, or read the sample online here

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

In fact, authors have earned over $15 million writing, publishing and selling on Leanpub.

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub