Understanding Linux Memory Management: Segments, Paging, and Allocation
This article explains how Linux organizes a process's address space into text, data, and stack segments, describes virtual memory concepts such as paging, page tables, and page faults, and details the kernel's allocation, deallocation, and memory‑mapping mechanisms including brk, mmap, and the buddy allocator.
Linux memory management is designed to be simple and portable, allowing the same kernel to run on machines with similar memory‑management units. The article walks through the core concepts, system calls, and implementation details of the Linux memory subsystem.
Basic Concepts
Each Linux process has its own address space divided into three segments:
Text segment : contains executable code; it is read‑only and can be shared between processes.
Data segment : stores initialized variables, strings, arrays, and the BSS (uninitialized data) which is zero‑filled at load time.
Stack segment : grows downward from the top of the virtual address space; it holds function call frames, local variables, and the initial environment variables.
The heap, managed by the C library malloc, resides between the data segment and the stack and grows upward as memory is dynamically allocated.
Memory‑Mapped Files
Linux supports mapping files directly into a process's address space using mmap. This allows a file to be accessed as if it were a byte array in memory, enabling efficient sharing of libraries and inter‑process communication. The same physical page can be mapped into different virtual address spaces of multiple processes.
Key System Calls
The kernel provides several memory‑management system calls: brk: adjusts the end of the data segment to increase or decrease the heap size. mmap / munmap: map or unmap files or anonymous memory regions. Parameters include addr (desired address, page‑aligned), len (size, also page‑aligned), prot (read/write/execute flags), flags (shared/private, etc.), fd (file descriptor), and offset (file offset).
Implementation Overview
The kernel uses virtual memory to give each process the illusion of a large, contiguous address space. Core ideas include:
Large address space : virtual addresses far exceed physical memory.
Protection : each page can be marked read‑only, writable, executable, or any combination.
Memory mapping : files are mapped into virtual address space, enabling shared libraries.
Fair physical allocation : pages are distributed evenly among processes.
Shared virtual memory : processes can communicate via shared pages.
Virtual Memory Model
Virtual addresses are divided into a page‑offset and a virtual page‑frame number. The processor translates a virtual address to a physical address using page tables. A page is a fixed‑size block (4 KB on x86, 8 KB on Alpha). Each page table entry stores a valid flag, the physical frame number, and access‑control bits.
When a process accesses an unmapped page, a page‑fault occurs. The kernel locates the corresponding vm_area_struct (organized in an AVL tree) to determine the allowed access. If the address is illegal, the kernel sends SIGSEGV to the process.
Caching Mechanisms
Buffer cache : caches block‑device data.
Page cache : caches file contents page‑by‑page.
Swap cache : keeps dirty pages that have been written to the swap file.
Hardware cache (TLB) : stores recent page‑table translations to avoid walking the page tables on every memory access.
Linux Page Tables
Linux uses a three‑level page‑table hierarchy (PGD → PUD → PMD → PTE). The top‑level PGD (global page directory) is created for each new process. Translating a virtual address requires walking each level to obtain the final physical frame number.
Page Allocation and Freeing
All physical pages are described by the mem_map array of mem_map_t structures, which track the reference count, age, and frame number of each page.
Allocation is performed by the buddy allocator ( free_area vectors). It searches for a free block of the requested order; if none is available, it looks for a larger block and splits it repeatedly until the desired size is obtained. The condition nr_free_pages > min_free_pages must hold before allocation proceeds.
When a page is freed, the allocator attempts to coalesce it with adjacent free buddies, merging them into larger blocks to reduce fragmentation.
Memory Mapping Types
Linux supports two mapping types:
Shared : changes are visible to all processes mapping the same region.
Private (copy‑on‑write) : writes create a private copy, leaving the original file unchanged.
Demand Paging
Only the parts of an executable that are actually accessed are loaded into physical memory. When a process accesses a page that is not present, the kernel handles the page fault, allocates a physical page, and updates the page tables.
Overall, Linux’s memory‑management subsystem combines virtual memory abstraction, sophisticated caching, and the buddy allocation algorithm to provide efficient, fair, and secure use of physical memory across all processes.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
