Fundamentals 33 min read

Unlocking Linux Dynamic Memory Management: How the Kernel Allocates and Reclaims Memory

This article explores Linux's dynamic memory management, covering virtual and physical memory, user and kernel space separation, paging and segmentation, mmap and brk allocation methods, the slab allocator, malloc/free behavior, and the system's memory reclamation strategies including swap usage.

Deepin Linux
Deepin Linux
Deepin Linux
Unlocking Linux Dynamic Memory Management: How the Kernel Allocates and Reclaims Memory

1. Linux Memory Management Basics

Memory management is the core of the Linux operating system, ensuring efficient allocation and reclamation of memory resources to keep the system stable and performant.

1.1 Virtual Memory vs Physical Memory

Physical memory (RAM) provides fast storage directly accessible by the CPU, while virtual memory creates the illusion of a large, contiguous address space for each process, using disk swap space when RAM is insufficient.

1.2 User Space vs Kernel Space

In a 32‑bit Linux system, the upper 1 GB of the 4 GB virtual address space is reserved for kernel space, while the lower 3 GB is allocated to user space processes, providing isolation and protection.

2. Principles of Dynamic Memory Management

2.1 Paging Mechanism

Linux divides memory into fixed‑size pages (typically 4 KB) and page frames, allowing any page to be placed in any free frame, which improves utilization and avoids fragmentation.

The paging mechanism is enabled when the CR0.PG bit is set to 1; otherwise linear addresses map directly to physical addresses.

CR0.PG = 0: paging disabled.

CR0.PG = 1: paging enabled.

2.2 Segmentation (Brief)

Segmentation divides a process's address space into logical segments (code, data, stack). Modern Linux keeps segment bases at zero, effectively delegating most protection duties to paging.

2.3 Memory Mapping (mmap) and brk

mmap

creates a new mapping in a process's virtual address space, allowing files or anonymous memory to be accessed like regular RAM, supporting shared and private mappings. brk moves the end of the data segment to expand or shrink the heap, providing a contiguous memory region.

2.4 Slab Allocator

The slab allocator manages small kernel objects by maintaining caches of fixed‑size objects, reducing fragmentation and improving allocation speed. Freed objects are returned to their cache for reuse.

3. Dynamic Allocation and Release Process

3.1 malloc Analysis

The C library function void *malloc(size_t size) obtains memory from glibc's internal pools; if the pool lacks sufficient space, it falls back to brk for small allocations or mmap for larger ones, typically using a 128 KB threshold.

malloc maintains free lists and may split larger free blocks to satisfy a request, later coalescing adjacent free blocks to reduce fragmentation.

3.2 free Analysis

void free(void *ptr)

returns a previously allocated block to the runtime library, which marks it free and links it back onto the free list. The memory is returned to the kernel only when large contiguous regions become free.

Common pitfalls include double free, use‑after‑free, and forgetting to nullify pointers after freeing.

4. Memory Reclamation Mechanisms

4.1 Triggers for Reclamation

Reclamation activates when memory pressure rises near a configured low‑water mark or when a new allocation cannot be satisfied.

4.2 Anonymous vs File Page Reclamation

Anonymous pages are swapped out to disk when idle, while clean file pages can be dropped directly; dirty file pages must be written back before being reclaimed.

4.3 Role and Principle of Swap Space

Swap provides a backing store for pages that cannot reside in RAM. Swapped‑out pages are written to swap and later read back when accessed, at the cost of slower I/O.

5. Characteristics and Advantages of Linux Dynamic Memory Management

5.1 Flexibility and Efficiency

The system allocates and frees memory on demand, supporting a wide range of allocation sizes from tiny objects (via slab) to large blocks (via buddy system), adapting to varying workload requirements.

5.2 Fragmentation Handling Strategies

The buddy system splits and merges power‑of‑two blocks to minimize external fragmentation, while the slab allocator reduces internal fragmentation by reusing fixed‑size objects from caches.

memory managementLinuxdynamic allocationSlab AllocatorPaging
Deepin Linux
Written by

Deepin Linux

Research areas: Windows & Linux platforms, C/C++ backend development, embedded systems and Linux kernel, etc.

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.