High Memory Usage — Linux Troubleshooting Guide
Diagnose high memory usage on Linux. Covers understanding free vs available memory, finding memory-hungry processes, detecting leaks, managing swap, and cache behavior.
Get a clear picture of total, used, free, and available memory.
free -h && echo '---' && cat /proc/meminfo | head -10
Identify which processes are using the most RAM.
ps aux --sort=-%mem | head -15 && echo '---' && ps aux --sort=-rss | awk 'NR<=10{printf "%-8s %-8s %6s MB %s\n", $1, $2, $6/1024, $11}'A process with steadily growing memory over time likely has a leak.
# Check RSS over time for a specific PID: while true; do ps -p PID -o rss=,vsz=,comm= && sleep 5; done # Or use smem for accurate per-process breakdown: smem -rkt -s pss | head -15 2>/dev/null || echo 'smem not installed (apt install smem)'
High swap usage means physical RAM was exhausted. Swapping causes severe performance degradation.
swapon -s && echo '---' && free -h | grep Swap && echo '---' && vmstat 1 3 | tail -3
Identify which processes have been swapped out.
for f in /proc/[0-9]*/status; do awk '/VmSwap/{swap=$2} /Name/{name=$2} END{if(swap>0) printf "%8d KB %s\n",swap,name}' $f; done 2>/dev/null | sort -rn | head -15Kernel slab allocators (dentry cache, inode cache) can consume significant memory.
slabtop -o -s c 2>/dev/null | head -15 || cat /proc/meminfo | grep -E 'Slab|SReclaimable|SUnreclaim'
Force the kernel to drop file caches. Only for emergencies — the system will rebuild caches and may slow down temporarily.
# Check reclaimable cache:
free -h | awk '/Mem/{print "Reclaimable cache: " $6}'
# Drop page cache, dentries, and inodes:
sync && echo 3 > /proc/sys/vm/drop_caches && free -hApply the appropriate fix based on your diagnosis.
# Kill a runaway process: kill -15 PID # graceful kill -9 PID # forced # Restart a leaking service: systemctl restart SERVICE_NAME # Set memory limits for a service: systemctl edit SERVICE_NAME # Add: [Service]\nMemoryMax=2G\nMemoryHigh=1.5G