Linux Memory Management

Linux memory management is a complex system that efficiently allocates and manages system RAM, virtual memory, and swap space. Understanding how memory works is crucial for system performance and troubleshooting.

Linux Memory Architecture

┌─────────────────────────────────────┐
│ User Space │
│ ┌─────────┐ ┌─────────┐ ┌────────┐ │
│ │ Process │ │ Process │ │ Process│ │
│ │ A │ │ B │ │ C │ │
│ └─────────┘ └─────────┘ └────────┘ │
└─────────────────────────────────────┘
┌─────────────────────────────────────┐
│ Kernel Space │
│ ┌─────────────────────────────────┐ │
│ │ Memory Management │ │
│ │ (Virtual Memory Manager) │ │
│ └─────────────────────────────────┘ │
└─────────────────────────────────────┘
┌─────────────────────────────────────┐
│ Physical Memory (RAM) │
│ ┌─────┐ ┌─────┐ ┌─────┐ ┌─────┐ │
│ │Page │ │Page │ │Page │ │Page │ │
│ │ 1 │ │ 2 │ │ 3 │ │ 4 │ │
│ └─────┘ └─────┘ └─────┘ └─────┘ │
└─────────────────────────────────────┘
┌─────────────────────────────────────┐
│ Swap Space (Disk) │
└─────────────────────────────────────┘

Overview

Linux memory management involves several key concepts:

  • Physical Memory (RAM) - Actual hardware memory installed in the system
  • Virtual Memory - Abstraction layer that provides each process with its own memory space
  • Swap Space - Disk storage used as overflow when RAM is full
  • Page Cache - Memory used to cache file system data
  • Buffer Cache - Memory used to cache block device data
  • Memory Mapping - Technique to map files or devices into memory

Memory Types and Usage

Memory Type Description Purpose Reclaimable
Used Memory actively used by processes Running applications and kernel No
Free Completely unused memory Available for immediate allocation N/A
Available Memory available for applications Free + reclaimable memory N/A
Buffers Memory used for block device I/O Disk block caching Yes
Cached Memory used for file system cache File content caching Yes
Shared Memory shared between processes Inter-process communication Depends
Swap Virtual memory on disk Overflow when RAM is full Yes

Checking Memory Usage

Basic memory information

free # Basic memory usage free -h # Human-readable format free -m # Show in megabytes free -s 5 # Update every 5 seconds

The free command shows overall system memory usage

Detailed memory information

cat /proc/meminfo # Detailed memory statistics cat /proc/meminfo | grep -E "(MemTotal|MemFree|MemAvailable|Buffers|Cached)"

Comprehensive memory information from the kernel

Process memory usage

top # Interactive process viewer htop # Enhanced interactive viewer ps aux --sort=-%mem # Processes sorted by memory usage ps aux | head -1; ps aux | sort -rn -k 4 | head -10

View memory usage by individual processes

Memory usage by specific process

pmap -d PID # Memory map of process cat /proc/PID/status # Process memory details cat /proc/PID/smaps # Detailed memory mapping

Detailed memory analysis for specific processes

Understanding free Command Output

$ free -h total used free shared buff/cache available Mem: 7.7G 2.1G 1.2G 156M 4.4G 5.2G Swap: 2.0G 0B 2.0G
Memory Fields Explained
  • total - Total installed RAM
  • used - Memory used by processes and kernel
  • free - Completely unused memory
  • shared - Memory used by tmpfs and shared memory
  • buff/cache - Memory used for buffers and cache
  • available - Memory available for starting new applications
Key Points
  • Linux uses "free" memory for caching to improve performance
  • Cached memory is automatically freed when applications need it
  • Focus on "available" rather than "free" for actual available memory
  • High cache usage is normal and beneficial for performance

Swap Space Management

View swap usage

free -h # Shows swap in summary swapon --show # List active swap devices cat /proc/swaps # Detailed swap information

Check current swap space usage and configuration

Create swap file

# Create 2GB swap file sudo fallocate -l 2G /swapfile sudo chmod 600 /swapfile sudo mkswap /swapfile sudo swapon /swapfile # Make permanent in /etc/fstab echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

Create and activate a swap file

Manage swap

sudo swapon /dev/sdb1 # Enable swap partition sudo swapoff /dev/sdb1 # Disable swap partition sudo swapon -a # Enable all swap in fstab sudo swapoff -a # Disable all swap

Enable and disable swap devices

Adjust swappiness

# View current swappiness (0-100) cat /proc/sys/vm/swappiness # Set swappiness temporarily sudo sysctl vm.swappiness=10 # Set permanently in /etc/sysctl.conf echo 'vm.swappiness=10' | sudo tee -a /etc/sysctl.conf

Control how aggressively the kernel swaps memory to disk

Memory Monitoring Tools

Real-time monitoring

top # Classic process monitor htop # Enhanced process monitor atop # Advanced system monitor glances # Cross-platform monitor

Interactive tools for real-time memory monitoring

System statistics

vmstat 1 # Virtual memory statistics vmstat 1 10 # Update every second, 10 times iostat -m 1 # I/O and memory statistics sar -r 1 10 # Memory utilization report

Command-line tools for detailed memory statistics

Memory pressure indicators

# Check for OOM (Out of Memory) events dmesg | grep -i "killed process" journalctl -k | grep -i "killed process" # Check memory pressure cat /proc/pressure/memory

Identify memory pressure and out-of-memory conditions

Process Memory Analysis

Memory usage by process

# Top memory consumers ps aux --sort=-%mem | head -10 # Memory usage in MB ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem | head -10 # Specific process memory ps -p PID -o pid,vsz,rss,comm

Identify processes consuming the most memory

Detailed process memory

# Memory map of process pmap -d PID pmap -x PID # Extended information # Process memory details cat /proc/PID/status | grep -E "(VmSize|VmRSS|VmData|VmStk)"

Analyze detailed memory usage of specific processes

Memory leaks detection

# Monitor process memory over time while true; do ps -p PID -o pid,vsz,rss,comm sleep 10 done # Use valgrind for detailed analysis valgrind --tool=memcheck --leak-check=full ./program

Monitor for memory leaks in running processes

Memory Optimization

Clear caches

# Clear page cache sudo sync && echo 1 | sudo tee /proc/sys/vm/drop_caches # Clear dentries and inodes sudo sync && echo 2 | sudo tee /proc/sys/vm/drop_caches # Clear all caches sudo sync && echo 3 | sudo tee /proc/sys/vm/drop_caches

Free up memory used by system caches (use with caution)

Kill memory-intensive processes

# Kill process by PID kill PID kill -9 PID # Force kill # Kill process by name killall process_name pkill process_name # Kill processes using most memory ps aux --sort=-%mem | head -5

Terminate processes to free up memory

Tune kernel parameters

# Adjust overcommit behavior echo 2 | sudo tee /proc/sys/vm/overcommit_memory # Set overcommit ratio echo 80 | sudo tee /proc/sys/vm/overcommit_ratio # Adjust dirty page writeback echo 15 | sudo tee /proc/sys/vm/dirty_background_ratio echo 30 | sudo tee /proc/sys/vm/dirty_ratio

Tune kernel memory management parameters

Memory Troubleshooting

Out of Memory (OOM) issues

# Check OOM killer logs dmesg | grep -i "out of memory" journalctl -k | grep -i "oom" # Check which processes were killed grep -i "killed process" /var/log/kern.log # Monitor memory pressure cat /proc/pressure/memory

Investigate out-of-memory conditions and killed processes

High memory usage

# Find memory hogs ps aux --sort=-%mem | head -10 # Check for memory leaks top -o %MEM # Analyze process memory growth watch -n 5 'ps aux --sort=-%mem | head -10'

Identify and analyze high memory usage patterns

Swap thrashing

# Monitor swap activity vmstat 1 # Check swap usage by process for file in /proc/*/status; do awk '/VmSwap|Name/{printf $2 " " $3}END{ print ""}' $file done | sort -k 2 -n # Reduce swappiness sudo sysctl vm.swappiness=1

Diagnose and resolve excessive swapping issues

Memory Management Best Practices

Monitoring Guidelines
  • Monitor memory usage regularly with tools like htop and free
  • Set up alerts for high memory usage (>80-90%)
  • Track memory trends over time to identify patterns
  • Monitor swap usage - high swap usage indicates memory pressure
  • Check for memory leaks in long-running processes
  • Use system monitoring tools like Nagios, Zabbix, or Prometheus
Optimization Strategies
  • Add more RAM if consistently running out of memory
  • Optimize applications to use less memory
  • Use memory-efficient alternatives for resource-heavy applications
  • Configure appropriate swap space (1-2x RAM for systems with <8GB RAM)
  • Tune swappiness based on workload (lower for servers, higher for desktops)
  • Use memory compression (zswap, zram) for better memory utilization
Important Warnings
  • Don't clear caches unnecessarily - they improve performance
  • Be careful with drop_caches - it can temporarily hurt performance
  • Don't set swappiness to 0 - it can cause OOM conditions
  • Monitor after changes - memory tuning can have unexpected effects
  • Test in non-production first - memory changes can affect stability

Advanced Memory Management

Memory compression

# Enable zswap (compressed swap cache) echo Y | sudo tee /sys/module/zswap/parameters/enabled # Check zswap status grep -r . /sys/kernel/debug/zswap/ # Configure zram (compressed RAM disk) sudo modprobe zram echo 1G | sudo tee /sys/block/zram0/disksize sudo mkswap /dev/zram0 sudo swapon /dev/zram0

Use memory compression to increase effective memory capacity

Memory cgroups

# Create memory cgroup sudo mkdir /sys/fs/cgroup/memory/mygroup # Set memory limit (1GB) echo 1073741824 | sudo tee /sys/fs/cgroup/memory/mygroup/memory.limit_in_bytes # Add process to cgroup echo PID | sudo tee /sys/fs/cgroup/memory/mygroup/cgroup.procs # Monitor cgroup memory usage cat /sys/fs/cgroup/memory/mygroup/memory.usage_in_bytes

Control memory usage of process groups using cgroups

Huge pages

# Check huge page support cat /proc/meminfo | grep -i huge # Configure huge pages echo 1024 | sudo tee /proc/sys/vm/nr_hugepages # Mount hugetlbfs sudo mkdir /mnt/huge sudo mount -t hugetlbfs nodev /mnt/huge # Check huge page usage cat /proc/meminfo | grep -i huge

Configure huge pages for applications requiring large memory allocations

Memory Performance Tuning

Kernel parameters

# Memory overcommit settings vm.overcommit_memory = 1 # Allow overcommit vm.overcommit_ratio = 50 # Overcommit ratio # Swapping behavior vm.swappiness = 10 # Reduce swap usage vm.vfs_cache_pressure = 50 # Cache pressure # Dirty page handling vm.dirty_background_ratio = 5 # Background writeback vm.dirty_ratio = 10 # Synchronous writeback # Add to /etc/sysctl.conf for persistence

Tune kernel memory management parameters for optimal performance

Application-specific tuning

# Database memory tuning (MySQL) innodb_buffer_pool_size = 70% # of available RAM # Java application tuning -Xms2g -Xmx4g # Initial and max heap -XX:+UseG1GC # Garbage collector # Web server tuning (Apache) MaxRequestWorkers = 150 # Based on available memory

Configure applications for optimal memory usage

Memory Monitoring Scripts

Memory usage alert script

#!/bin/bash # memory_alert.sh THRESHOLD=90 USAGE=$(free | grep Mem | awk '{printf("%.0f", $3/$2 * 100)}') if [ $USAGE -gt $THRESHOLD ]; then echo "WARNING: Memory usage is ${USAGE}%" ps aux --sort=-%mem | head -10 fi

Simple script to alert on high memory usage

Memory report script

#!/bin/bash # memory_report.sh echo "=== Memory Usage Report ===" echo "Date: $(date)" echo echo "Overall Memory Usage:" free -h echo echo "Top 10 Memory Consumers:" ps aux --sort=-%mem | head -11 echo echo "Swap Usage:" swapon --show

Generate comprehensive memory usage reports

See also