Fundamentals 19 min read

Unlocking Linux Memory: From Basics to Advanced Allocation Strategies

This comprehensive guide explains Linux memory fundamentals, address space layout, fragmentation, the buddy and slab allocators, kernel and user‑mode memory pools, DMA handling, common programming pitfalls, and practical tools for monitoring memory usage.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Unlocking Linux Memory: From Basics to Advanced Allocation Strategies

1. Introduction to Linux Memory

Memory, also called main memory, is the directly addressable storage for the CPU, built from semiconductor devices. It provides fast data access, temporarily stores computation results, exchanges data with external storage, and ensures stable high‑performance operation of the CPU.

2. Linux Memory Address Space

Linux separates memory into user space and kernel space. User‑mode code runs in Ring 3 and cannot directly access kernel addresses, while kernel‑mode code runs in Ring 0 and shares a single address space among all kernel threads.

The Memory Management Unit (MMU) translates logical addresses to linear addresses via segmentation and then linear addresses to physical addresses via paging. In a 32‑bit system, paging uses a 10‑bit page directory, a 10‑bit page table, and a 12‑bit offset, yielding 4 KB pages.

2.1 Segmentation

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

Each segment has a base address and a limit defining the maximum offset.

2.2 Paging

MMU contains a segment part and a page part.

Linear addresses are mapped to physical pages.

3. Linux Memory Allocation Algorithms

3.1 Fragmentation

Frequent small allocations with long lifetimes create external fragmentation, reducing usable memory and performance.

3.2 Buddy System

The buddy allocator provides efficient allocation of contiguous pages, maintaining 11 free‑list bins for block sizes from 1 to 1024 pages (up to 4 MB). Allocation searches the appropriate bin; if empty, larger bins are split. Deallocation merges buddies when possible.

3.3 Slab Allocator

Derived from the SunOS slab algorithm, the Linux slab allocator caches frequently used kernel objects (e.g., process descriptors). It reduces internal fragmentation and speeds up object creation. Slabs are organized into caches; empty slabs are reclaimed, and objects are initialized only once.

3.4 Kernel Memory Pools

Memory pools pre‑allocate a set of equal‑sized blocks. When a request arrives, a block is taken from the pool; if the pool is empty, new blocks are allocated. This reduces fragmentation for high‑frequency allocations.

3.5 DMA‑Capable Memory

Direct Memory Access (DMA) allows peripherals to read/write main memory without CPU intervention. The DMA controller handles bus arbitration, address translation, transfer size, and completion signaling (DREQ, DACK, HRQ, HLDA).

4. Memory Usage Scenarios

Page management (kernel page allocator).

Slab/kmalloc and memory pools for kernel objects.

User‑mode allocation: malloc, calloc, realloc, alloca.

Process memory map: stack, heap, code, data.

Kernel‑user data transfer (copy_from_user, copy_to_user).

Memory‑mapped I/O and reserved regions.

DMA buffers.

5. Common Pitfalls

5.1 C Memory Leaks

Mismatched new/delete in constructors/destructors.

Missing deletion of nested pointers.

Non‑virtual base destructors.

Absent copy constructors leading to shallow copies.

Improper handling of pointer arrays versus object arrays.

Missing overloaded assignment operators.

5.2 Wild Pointers

Uninitialized pointers.

Use‑after‑free without resetting to NULL.

Returning pointers to stack memory.

Dereferencing NULL.

Incorrect sizeof usage on arrays.

5.3 Resource Conflicts

Missing volatile on shared variables.

Unsynchronized access to global data.

Improper use of shared memory without locks.

Unsafe mmap usage across processes.

5.4 STL Iterator Invalidation

Erasing an element invalidates the iterator; store the next iterator before erase.

5.5 Smart Pointer Misuse

Replace deprecated auto_ptr with unique_ptr.

Use make_shared to create shared_ptr.

Understand weak_ptr semantics (lock, expired, get).

5.6 Modern C++ Improvements

std::atomic

for lock‑free synchronization. std::array for fixed‑size arrays. std::vector::shrink_to_fit() to reduce capacity. std::forward_list as a memory‑efficient singly linked list. std::unordered_map/set for O(1) look‑ups.

6. Monitoring Memory

System memory: /proc/meminfo Process memory: /proc/<pid>/status Overall usage: free CPU/Memory per process: top Virtual memory stats: vmstat Process memory ranking: ps aux --sort -rss Drop caches: echo 1 > /proc/sys/vm/drop_caches (pagecache), echo 2 > /proc/sys/vm/drop_caches (dentries/inodes), echo 3 > /proc/sys/vm/drop_caches (both).

Images

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 ManagementKernelLinuxDMAmemory allocationbuddy systemSlab Allocator
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.