Memory & Performance Management

25 minLesson 8 of 16

Learning Objectives

  • Check memory usage with free, top, and /proc/meminfo
  • Understand physical memory, virtual memory, and caching
  • Manage swap space and clear caches
  • Use vmstat to monitor system performance over time
  • Identify and terminate memory-intensive processes

Types of Memory in Linux

TypeDescription
Physical (RAM)Actual hardware memory installed
VirtualAbstraction layer mapping to physical addresses
SwapDisk space used when RAM is full
Cache/BuffersRAM used to speed up disk operations

Checking Memory Usage

The free Command

free -h

Output:

              total        used        free      shared  buff/cache   available
Mem:          3.8Gi       425Mi       3.1Gi       0.0Ki       246Mi       3.1Gi
Swap:            0B          0B          0B
ColumnMeaning
totalTotal installed RAM
usedRAM actively in use
freeCompletely unused RAM
buff/cacheRAM used for caching (reclaimable)
availableRAM available for new processes

Key insight: Linux uses free RAM for caching to speed up operations. The available column shows what's truly available — not free.

The /proc/meminfo File

For detailed memory statistics:

# Overview
cat /proc/meminfo | grep "Mem"
 
# Swap info
cat /proc/meminfo | grep "Swap"
 
# Buffer and cache details
cat /proc/meminfo | grep -e "Buffers" -e "Cached"

Clearing Caches

In rare cases, you may need to free cached memory:

# Sync pending writes to disk first
sync
 
# Clear page cache, dentries, and inodes
sudo sh -c 'echo 3 > /proc/sys/vm/drop_caches'
 
# Verify
free -h
ℹ️

Clearing caches is rarely needed in production. Linux manages memory efficiently — caches improve performance and are released automatically when applications need RAM.

Swap Management

Swap is disk space used as overflow when RAM is exhausted. It's much slower than RAM.

Check Swap Status

# View swap usage
free -h | grep Swap
 
# Detailed swap info
swapon --show

Create a Swap File

# Create a 2GB swap file
sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
 
# Verify
free -h
 
# Make persistent (add to /etc/fstab)
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

Swappiness

Controls how aggressively Linux uses swap (0-100):

# Check current value
cat /proc/sys/vm/swappiness
 
# Set to 10 (prefer RAM, use swap only when necessary)
sudo sysctl -w vm.swappiness=10
 
# Make persistent
echo 'vm.swappiness=10' | sudo tee -a /etc/sysctl.conf

Monitoring with vmstat

vmstat provides a snapshot of system performance:

# Single snapshot
vmstat
 
# Update every 2 seconds, 10 times
vmstat 2 10
 
# With timestamps
vmstat -t 2 5
 
# Memory summary
vmstat -s

Key columns:

ColumnMeaning
rProcesses waiting for CPU
bProcesses in uninterruptible sleep
swpdVirtual memory used (swap)
freeFree RAM
si/soSwap in/out (high = problem)
usUser CPU time
sySystem CPU time
idIdle CPU time
waWaiting for I/O

Red flags: High si/so means excessive swapping. High wa means disk bottleneck.

Process Management for Performance

Finding Resource-Hungry Processes

# Top CPU consumers
ps aux --sort=-%cpu | head -10
 
# Top memory consumers
ps aux --sort=-%mem | head -10
 
# Real-time monitoring
top
# Press M to sort by memory, P to sort by CPU

Killing Problematic Processes

# Find the PID
ps aux | grep "problematic_app"
 
# Graceful termination
kill PID
 
# Force kill if unresponsive
kill -9 PID

Process Priority (nice/renice)

Adjust CPU priority for processes:

# Start a process with low priority (higher nice = lower priority)
nice -n 19 ./heavy_computation.sh
 
# Change priority of running process
renice -n 10 -p PID

Nice values range from -20 (highest priority) to 19 (lowest priority).

Practical: Server Health Script

#!/bin/bash
# server_health.sh
 
echo "=== Memory Status ==="
free -h | grep Mem | awk '{printf "Used: %s / Total: %s (Available: %s)\n", $3, $2, $7}'
 
echo ""
echo "=== Swap Status ==="
free -h | grep Swap | awk '{printf "Used: %s / Total: %s\n", $3, $2}'
 
echo ""
echo "=== Top 5 Memory Consumers ==="
ps aux --sort=-%mem | head -6 | awk '{printf "%-10s %5s%% %s\n", $1, $4, $11}'
 
echo ""
echo "=== CPU Load (1/5/15 min) ==="
uptime | awk -F'load average:' '{print $2}'

Summary

  • free -h shows memory overview; available is the key metric
  • Linux caches aggressively — this is normal and beneficial
  • Swap is emergency overflow; high swap usage indicates insufficient RAM
  • vmstat monitors system performance over time
  • High si/so = swap thrashing, high wa = disk bottleneck
  • Use ps aux --sort=-%mem to find memory hogs
  • nice/renice adjust process CPU priority

Next Steps

Next, we'll cover storage management — partitioning disks, creating file systems, and managing mount points.