Fundamentals 19 min read

Unlock Linux Memory: From Address Spaces to Allocation Algorithms

This article provides a comprehensive guide to Linux memory management, covering memory organization, address spaces, MMU translation, allocation algorithms such as the buddy system and slab allocator, usage scenarios, common pitfalls, and practical tools for monitoring and debugging memory usage.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Unlock Linux Memory: From Address Spaces to Allocation Algorithms

1. Introduction to Linux Memory

Memory, also known as main memory, is the storage space directly addressable by the CPU, characterized by fast access speed. Proper memory usage improves system performance and stability.

2. Linux Memory Address Space

2.1 Overall Layout

Linux divides memory into user space and kernel space. Each process has an isolated virtual address space, while the kernel shares a common address space.

2.2 User and Kernel Modes

User mode (Ring3) runs application code with limited privileges.

Kernel mode (Ring0) has full access to hardware and memory.

Transitions from user to kernel occur via system calls, exceptions, or hardware interrupts.

2.3 MMU Address Translation

The MMU consists of segmentation and paging units.

Segmentation converts a logical address to a linear address.

Paging converts the linear address to a physical address.

2.4 Segmentation Details

Segment selectors are cached in six segment registers: cs, ss, ds, es, fs, gs.

Each segment has a base address and a limit defining its range.

2.5 Paging (32‑bit)

Page directory (10 bits), page table (10 bits), and page offset (12 bits) form a 4 KB page.

2.6 User‑Space Layout

TEXT: executable code and read‑only data.

DATA: initialized global variables.

BSS: uninitialized global variables.

HEAP: dynamically allocated memory (malloc, new).

MMAP: memory‑mapped files and shared libraries.

STACK: process stack.

2.7 Kernel‑Space Layout

Direct‑mapped region (≈896 MB starting at 3 GB).

Dynamic vmalloc region.

Permanent high‑memory mapping.

Fixed mappings for specific hardware (e.g., ACPI).

2.8 Process Memory Space

User processes can only access their own virtual address space.

Kernel space is managed separately and is not directly accessible by user processes.

3. Linux Memory Allocation Algorithms

3.1 Memory Fragmentation

Fragmentation occurs when many small allocations with long lifetimes leave unusable gaps, reducing overall utilization.

3.2 Buddy System

Divides free pages into 11 size classes (1 to 1024 pages).

Allocation requests 2^i pages; if unavailable, higher order blocks are split.

When freeing, adjacent buddies of the same order are merged.

3.3 Slab Allocator

Provides caches for frequently used kernel objects, reducing allocation overhead.

Supports both generic caches (kmalloc) and specialized caches via kmem_cache_create.

3.4 Kernel Memory Pools

Pre‑allocate a pool of equal‑sized blocks for fast allocation.

APIs: mempool_create, mempool_alloc, mempool_free, mempool_destroy.

3.5 Large Allocation Strategies

Modify MAX_ORDER and recompile the kernel for >4 MB allocations.

Use boot parameters (e.g., mem=80M) or ioremap for reserved memory.

vmalloc for non‑contiguous physical memory.

4. Memory Usage Scenarios

Page management (page, slab, kmalloc, memory pools).

User‑space allocation (malloc, realloc, calloc, mmap).

Process memory map (stack, heap, code, data).

Data transfer between kernel and user space (copy_from_user, copy_to_user).

Memory‑mapped hardware registers and reserved regions.

DMA memory handling.

5. Common Pitfalls

5.1 C Memory Leaks

Missing matching new/delete in constructors/destructors.

Non‑virtual base class destructors.

Improper handling of pointer arrays.

5.2 Wild Pointers

Uninitialized pointers.

Use‑after‑free without resetting to NULL.

5.3 Resource Conflicts

Shared variables without volatile or proper locking.

Unsynchronized access to shared memory.

5.4 STL Iterator Invalidations

Erasing elements invalidates iterators; store next iterator before erase.

5.5 Modern C++ Practices

Replace auto_ptr with unique_ptr.

Use make_shared for shared_ptr.

Leverage std::atomic, std::array, std::vector, forward_list, unordered_map/set for performance.

6. Monitoring and Debugging Memory

System-wide info: /proc/meminfo Process status: /proc/<pid>/status Memory usage summary: free Top processes: top Virtual memory stats: vmstat Sort processes by RSS: ps aux --sort -rss Drop caches: echo 1 > /proc/sys/vm/drop_caches (pagecache), echo 2 > ... (dentries/inodes), echo 3 > ... (both).

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Memory ManagementLinuxAllocation Algorithms
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

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.