Fundamentals 13 min read

How Linux Manages Memory: From Physical Pages to Virtual Address Mapping

This article explains Linux's memory management architecture, covering physical memory organization, the buddy and SLUB allocators, virtual address spaces for user and kernel, page‑table translation, TLB caching, and the role of swap in virtual memory.

Liangxu Linux
Liangxu Linux
Liangxu Linux
How Linux Manages Memory: From Physical Pages to Virtual Address Mapping

Memory Management Overview

Linux manages physical memory by organizing it into hierarchical structures and provides allocation and reclamation services. To protect the kernel and isolate processes, Linux introduces virtual addresses that are translated to physical addresses by the CPU.

Virtual Address Purpose

Logical (virtual) addresses prevent user processes from directly accessing kernel memory or other processes' memory. The CPU translates these logical addresses to physical addresses via page tables, giving each process a private address range.

Physical Memory Organization

Page : basic allocation unit, typically 4 KB.

Zone : groups pages into queues based on usage constraints. Common zones are ZONE_DMA (DMA‑capable devices), ZONE_NORMAL (kernel data), and ZONE_HIGHMEM (user‑space data on systems with high memory).

Node : each CPU has an associated node that contains one instance of each zone. When a node exhausts its memory, the allocator can obtain pages from another node.

Physical memory hierarchy
Physical memory hierarchy

Physical Memory Allocation

Buddy System (large allocations) : Pages are kept in free lists whose sizes are powers of two (1, 2, 4, …, 1024 pages). When a request for 2^i pages arrives, the allocator searches the corresponding list; if none is available it splits a larger block, updating the free lists accordingly.

SLUB Allocator (small allocations) : SLUB creates per‑CPU caches of one or a few pages. Each cache maintains a linked list of free objects; allocation and deallocation are performed by popping or pushing objects without zero‑clearing each time.

Buddy and SLUB allocation
Buddy and SLUB allocation

Virtual Address Space Organization

Virtual address space is divided into user space and kernel space.

User‑Space Layout

Text segment (executable code)

Data segment (initialized globals)

BSS segment (zero‑initialized globals)

Heap (dynamic allocations via malloc, brk, mmap)

Stack (function call frames)

Mmap region (memory‑mapped files and anonymous mappings)

User‑space layout
User‑space layout

Kernel‑Space Layout

Direct mapping area (~896 MB): a fixed‑offset region that maps ZONE_DMA and ZONE_NORMAL pages directly to virtual addresses.

Dynamic mappings : flexible mappings that can bind any virtual address to any physical page, required for accessing ZONE_HIGHMEM. Dynamic mappings come in three types:

Dynamic – can be re‑bound after use.

Permanent – one‑to‑one mapping that stays until explicitly unmapped.

Fixed – used by specific kernel functions.

Kernel‑space layout
Kernel‑space layout

Virtual‑to‑Physical Address Translation

Each process has its own page table; the kernel uses a single global page table. Both virtual and physical memory are divided into 4 KB pages.

On 32‑bit systems the translation uses a three‑level hierarchy:

10‑bit index into the first‑level page‑directory.

10‑bit index into the second‑level page‑table.

12‑bit offset within the 4 KB page.

64‑bit systems employ a five‑level page‑table hierarchy to cover the larger address space (each process can address up to 128 TB).

32‑bit page‑table layout
32‑bit page‑table layout
64‑bit page‑table layout
64‑bit page‑table layout

Translation Lookaside Buffer (TLB)

The TLB is a CPU‑resident cache that stores recent virtual‑to‑physical translations. A memory access first probes the TLB; on a miss the hardware walks the page tables to obtain the mapping and updates the TLB.

Virtual Memory (Swap)

When physical RAM is exhausted, Linux can move inactive pages to a swap partition on disk. Swapped‑out pages free physical memory, allowing programs to allocate more virtual memory than the available RAM, at the cost of slower disk I/O.

Key Takeaways

Linux separates memory into user and kernel virtual spaces and maps them to physical pages via per‑process page tables.

Physical memory is organized as pages → zones → nodes.

Large allocations use the buddy system; small allocations use SLUB.

Kernel space contains a direct‑mapping region for low memory and dynamic mappings for high memory.

Virtual‑to‑physical translation is performed by a multi‑level page‑table; the TLB caches recent translations.

Swap provides a disk‑backed extension of RAM, enabling larger virtual address spaces.

LinuxPage TablesBuddy systemSLUB
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.