Operations 27 min read

Master Linux Memory Management: Diagnose, Monitor & Optimize Performance

Learn how Linux memory management works, from virtual memory and user/kernel space to allocation, reclamation, and common bottlenecks, and discover practical tools like free, top, vmstat, valgrind, jstat, and swap configuration techniques to diagnose, monitor, and optimize server memory for stable, high‑performance operation.

Deepin Linux
Deepin Linux
Deepin Linux
Master Linux Memory Management: Diagnose, Monitor & Optimize Performance

Part 1: Linux Memory Management Mechanism

In the digital era, server stability directly impacts business. Memory shortages often cause server stalls because the system must swap data to disk, reducing efficiency.

Linux uses a virtual memory system where every user‑space address is translated to a physical address via page tables and hardware. The 4 GB virtual address space (on 32‑bit) is split roughly 3:1 between user space and kernel space.

1.1 User‑Space and Kernel‑Space Division

Processes run in user space with limited privileges; only the kernel can access kernel space. Switching requires system calls or interrupts.

1.2 Memory Allocation and Reclamation

In user space, allocation functions such as malloc, calloc, and realloc request memory from the kernel. In kernel space, kmalloc provides small contiguous physical blocks, vmalloc allocates larger virtual areas that may be non‑contiguous physically, and get_free_pages allocates whole pages.

When memory is no longer needed, free releases user‑space allocations and kfree releases kernel allocations. Background threads like kswapd reclaim unused pages.

Understanding virtual memory, page cache, and the slab allocator gives a “master key” for diagnosing server memory stalls.

Part 2: Precise Memory Problem Localization

Before optimization, identify the root cause using monitoring tools.

2.1 Memory Monitoring Tools

free shows total, used, free, shared, buff/cache, and available memory (e.g., free -h).

total – total system memory.

used – memory currently in use.

free – completely idle memory.

shared – memory shared among processes.

buff/cache – memory used for buffers and cache.

available – memory that can be allocated without swapping.

top provides a real‑time view; pressing M sorts by memory usage. Important fields: VIRT, RES, SHR, %MEM.

vmstat reports virtual memory, processes, CPU, and I/O statistics. Key fields: swpd, free, buff, cache, si, so.

2.2 Identifying Bottlenecks and Leaks

Memory bottleneck: sustained usage > 80 % or low available memory; high si/so values indicate frequent swapping.

Memory leak: a process’s memory consumption grows continuously without corresponding workload changes.

Part 3: Practical Optimization Strategies

3.1 Optimizing Allocation

Use malloc and free correctly. Example:

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

int main() {
    int n = 10;
    char *str = (char *)malloc((n + 1) * sizeof(char));
    if (str == NULL) {
        perror("malloc failed");
        return 1;
    }
    strcpy(str, "example");
    printf("String: %s
", str);
    free(str);
    str = NULL;
    return 0;
}

Tools such as memon monitor allocation frequency and help detect fragmentation. slabtop shows kernel slab caches; excessive objects in a slab may indicate inefficient allocation patterns.

3.2 Using Swap Space

Create a swap file:

sudo dd if=/dev/zero of=/swapfile bs=1G count=2
sudo mkswap /swapfile
sudo swapon /swapfile

Add to /etc/fstab for persistence:

/swapfile swap swap defaults 0 0

Adjust size based on physical RAM (e.g., 2 × RAM for < 2 GB, 2‑4 GB for larger systems).

3.3 Tuning Kernel Parameters

Reduce swap usage:

vm.swappiness = 10

Control dirty page write‑back:

vm.dirty_ratio = 30
vm.dirty_background_ratio = 10

Apply changes with sudo sysctl -p.

3.4 Cleaning Caches and Temporary Files

sudo sync
sudo echo 3 > /proc/sys/vm/drop_caches

Remove old files in /tmp and rotate logs as needed.

3.5 Optimizing Application Memory

Use valgrind --tool=memcheck to detect leaks, and perf to profile memory accesses.

# Example Valgrind usage
valgrind --tool=memcheck ./test

Part 4: Memory Tuning Tools

4.1 jstat – JVM Statistics

Monitor garbage collection and heap usage, e.g., jstat -gcutil 12345 1000 5.

4.2 jmap – Memory Map

Generate heap histograms ( jmap -histo pid) or dumps ( jmap -dump:format=b,file=heap.hprof pid) for analysis with MAT.

4.3 VisualVM – Visual Monitoring

Provides real‑time CPU, heap, and thread graphs; supports plugins for GC and thread analysis.

4.4 MAT – Memory Analyzer Tool

Analyzes heap dumps, identifies leak suspects, and visualizes object retention graphs.

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.

Performance OptimizationMemory ManagementLinuxMonitoring ToolsSwapKernel Tuning
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.