Fundamentals 37 min read

Comprehensive Guide to Linux Memory Management: Concepts, Techniques, and Code Examples

This extensive article explains Linux memory management fundamentals, covering physical and virtual memory, allocation and release mechanisms, key kernel functions, performance tuning, leak detection, fragmentation handling, and real‑world code samples for developers and system engineers.

Linux Tech Enthusiast
Linux Tech Enthusiast
Linux Tech Enthusiast
Comprehensive Guide to Linux Memory Management: Concepts, Techniques, and Code Examples

1. Overview of Linux Memory Management

Linux memory management handles allocation, release, mapping, swapping, and compression of system memory. The memory space is divided into kernel space, user space, caches, and swap partitions, aiming to maximize utilization while ensuring stability and reliability.

1.1 What is Memory Management?

Memory management is a core OS mechanism responsible for allocating, freeing, mapping, and virtual memory handling, which improves resource utilization and application performance.

1.2 Importance of Memory Management

System stability : prevents crashes caused by insufficient or leaked memory.

Performance : reduces fragmentation and improves efficiency.

Security : isolates processes and prevents malicious memory modifications.

Resource waste : avoids unused memory lingering.

Effective memory management is essential for reliable system operation.

1.3 Components of Memory Management

Virtual memory management : maps physical memory to independent process address spaces.

Physical memory management : allocates, frees, and maps physical pages.

Page‑replacement algorithms : select pages to evict when memory is scarce.

Process address‑space management : handles code, data, and stack segments.

Memory protection and access control : uses page attributes to enforce isolation.

Memory statistics and monitoring : provides data for tuning and troubleshooting.

2. Physical Memory Management

Physical memory is divided into fixed‑size pages (typically 4 KB or 8 KB). Allocation and release are performed via page pools.

2.2 Continuous Memory Management

Linux uses the Buddy System to manage contiguous physical memory. When a request arrives, the allocator finds the smallest suitable block; if the block is larger, it splits it into two equal buddies. Upon free, adjacent buddies are merged, reducing fragmentation.

2.2.2 Non‑Continuous Memory Management

Non‑contiguous allocation relies on paging or segmentation. Paging maps virtual pages to any physical page, while segmentation allows variable‑size segments with base and limit fields. Non‑contiguous methods are more flexible but incur higher hardware and software overhead.

2.3 Relevant Functions and Example

memblock_init()

: initialize physical memory blocks. memblock_reserve(): reserve blocks that must not be allocated. memblock_alloc(): allocate a block of physical memory. memblock_find_in_range(): locate a free block within a range.

#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/mm.h>

static int __init test_init(void)
{
    unsigned long size = 4096;
    unsigned long *ptr;
    ptr = memblock_alloc(size, PAGE_SIZE);
    if (!ptr) {
        pr_err("Failed to allocate memory
");
        return -ENOMEM;
    }
    pr_info("Allocated %ld bytes of physical memory at address %p
", size, ptr);
    return 0;
}

static void __exit test_exit(void)
{
    pr_info("Exiting test module
");
}

module_init(test_init);
module_exit(test_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Your Name");
MODULE_DESCRIPTION("Test module");

3. Virtual Memory Management

Virtual memory abstracts physical memory, allowing each process to see a contiguous address space while the underlying pages may be scattered. It enables larger address spaces than physical RAM and improves isolation and security.

3.1 What is Virtual Memory?

Virtual memory combines RAM with a portion of disk storage. Pages are typically 4 KB. When a process accesses a page not in RAM, the OS swaps it in; when the page is no longer needed, it may be swapped out.

3.2 Principles

Accesses to virtual addresses are translated by the Memory Management Unit (MMU) into physical addresses. If a page is missing, a page fault triggers allocation or swapping.

3.3 Functions and Examples

mmap()

: map a file or device into a process address space. munmap(): unmap a previously mapped region. mlock(): lock a virtual region in RAM. mprotect(): change protection flags of a mapped region.

#include <sys/mman.h>
#include <fcntl.h>
#include <stdio.h>

int main()
{
    int fd = open("file.txt", O_RDONLY);
    if (fd < 0) { perror("open"); return 1; }
    void *addr = mmap(NULL, 4096, PROT_READ, MAP_PRIVATE, fd, 0);
    if (addr == MAP_FAILED) { perror("mmap"); close(fd); return 1; }
    printf("%s
", (char *)addr);
    munmap(addr, 4096);
    close(fd);
    return 0;
}

4. Memory Allocation and Release

Allocation and release are fundamental OS services. In user space, the C library provides malloc(), calloc(), realloc(), and free(). In kernel space, functions such as kmalloc(), vmalloc(), and sbrk() are used.

4.1 Allocation Methods

Static allocation : determined at compile time.

Stack allocation : automatic for local variables.

Heap allocation : via malloc() and related functions.

Memory‑mapped files : using mmap() for large data.

Shared memory : inter‑process communication via shmget() or mmap() with MAP_SHARED.

4.2 Example: malloc()/free()

#include <stdlib.h>
#include <stdio.h>

int main()
{
    int *p = malloc(sizeof(int));
    if (!p) { printf("Failed to allocate memory!
"); return -1; }
    *p = 123;
    printf("%d
", *p);
    free(p);
    return 0;
}

4.3 Example: calloc()/realloc()

#include <stdlib.h>
#include <stdio.h>

int main()
{
    int *p1 = calloc(5, sizeof(int));
    if (!p1) { printf("Failed to allocate memory!
"); return -1; }
    for (int i = 0; i < 5; ++i) printf("%d ", p1[i]);
    printf("
");
    int *p2 = realloc(p1, 10 * sizeof(int));
    if (!p2) { printf("Failed to allocate memory!
"); return -1; }
    for (int i = 5; i < 10; ++i) p2[i] = i * 2;
    for (int i = 0; i < 10; ++i) printf("%d ", p2[i]);
    printf("
");
    free(p2);
    return 0;
}

5. Process Switching and Memory Management

During a context switch, the OS saves the current process's state and loads the next process's page tables, linking virtual memory to physical frames. This tight coupling ensures each process accesses its own memory safely.

5.1 Process Switching Overview

Switching involves saving registers, program counter, and stack pointer, then restoring the next process's context. It is resource‑intensive but essential for multitasking.

5.2 Interaction with Memory Management

The OS must preserve the current process's memory mappings and load the new process's mappings, updating the MMU accordingly.

5.3 Relevant Functions and Samples

fork()

: creates a child process with a copy‑on‑write memory image. exec(): replaces the current image with a new program. mmap() and munmap(): manage address‑space mappings during execution. malloc() and sbrk(): manipulate the heap.

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

int main()
{
    pid_t pid = fork();
    if (pid < 0) { perror("fork error"); exit(1); }
    else if (pid == 0) { printf("Child process
"); exit(0); }
    else { printf("Parent process
"); }
    return 0;
}

6. Tuning Linux Memory Management

6.1 Performance Tuning

Memory usage : adjust kernel parameters to improve utilization.

Swap configuration : size and swappiness to balance RAM and swap.

Mapping optimization : increase mmap cache size, tune access patterns.

Allocation tuning : enlarge allocation caches, select efficient algorithms.

6.2 Leak Detection and Debugging

Valgrind

: detects leaks, double frees, and invalid accesses. AddressSanitizer: runtime detection of memory errors. GDB: general debugging, can inspect memory state. LeakTracer: lightweight leak monitoring.

6.3 Fragmentation Reduction

Buddy system : merges free buddies to form larger blocks.

Memory pools : pre‑allocate objects to avoid fragmentation.

Memory compression : compress rarely used pages (e.g., zswap).

Alignment : allocate on natural boundaries to reduce waste.

7. Applications of Linux Memory Management

7.1 System‑level Use Cases

Servers: high‑load environments rely on efficient memory handling.

Embedded devices: limited RAM demands careful allocation.

Scientific computing: large datasets benefit from virtual memory and paging.

Kernel development: deep understanding required for stability.

Virtualization: hypervisors manage multiple guest memory spaces.

7.2 Driver Development

Character drivers use kmalloc() or vmalloc() for buffers.

Network drivers allocate pages via alloc_pages().

Block drivers employ memory‑mapped I/O.

Video drivers allocate large buffers with vmalloc().

7.3 System Optimization

Memory compression (zswap) to reduce RAM pressure.

Kernel reclaim mechanisms to free unused pages.

Transparent Huge Pages (THP) to lower TLB misses.

Proper swap sizing to avoid excessive paging.

8. Conclusion

The article introduced memory management concepts, detailed physical and virtual mechanisms, presented key kernel functions with runnable examples, discussed allocation strategies, process switching interactions, performance tuning, leak detection, fragmentation handling, and highlighted practical applications in servers, embedded systems, drivers, and system optimization.

Linux memory management diagram
Linux memory management diagram
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 managementKernelLinuxVirtual MemoryC ProgrammingPhysical Memory
Linux Tech Enthusiast
Written by

Linux Tech Enthusiast

Focused on sharing practical Linux technology content, covering Linux fundamentals, applications, tools, as well as databases, operating systems, network security, and other technical knowledge.

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.