How to Diagnose and Fix High Swap Usage on Linux Servers
Learn step‑by‑step how to identify why your Linux server’s swap is consuming over 90% of space, understand the performance impact, and apply three safe optimization techniques—including process analysis, swappiness tuning, and cache clearing—to restore smooth operation without rebooting.
Even when memory appears available, a server can become sluggish, time out, or have processes killed because swap usage is extremely high.
Step 1: Check Swap Usage
View overall Swap usage
free -h
# Example output
total used free shared buff/cache available
Mem: 15Gi 10Gi 1.2Gi 200Mi 4.0Gi 4.8Gi
Swap: 8.0Gi 7.5Gi 500MiCheck swap activity (critical): vmstat 1 5 Focus on the last two columns: si (swap‑in) and so (swap‑out). Normal: both stay at 0. Abnormal: persistently >0 indicates frequent paging.
Step 2: Find the Process Consuming Swap
Method 1: Sort processes by swap usage (recommended)
for file in /proc/*/status; do
awk '/VmSwap|Name/{printf $2 " " $3}END{ print ""}' $file;
done | sort -k 2 -n -r | head -10Example output shows java 2145632 kB, meaning the Java process is using about 2.1 GB of swap.
Method 2: Use smem tool (more visual)
# Install
yum install -y smem # CentOS/Rocky
apt install -y smem # Ubuntu/Debian
# Sort by swap
smem -s swap -r | head -10Step 3: Targeted Fixes
Case 1: Process memory leak (e.g., Java, Python)
Restart service (temporary relief)
Analyze memory dump (e.g., jmap -dump for Java)
Limit maximum memory (e.g., JVM -Xmx4g)Case 2: System memory sufficient but swap still used
Reason: Linux’s default swappiness (60) makes it use swap aggressively.
# Temporary change
sysctl vm.swappiness=10
# Permanent change
echo 'vm.swappiness=10' >> /etc/sysctl.confSuggested values: databases/high‑performance services 1‑10, normal servers 30, never use swap 0 (only if memory is ample).
Case 3: Large cache triggers swap
# Release pagecache (safe)
echo 1 > /proc/sys/vm/drop_caches
# Release dentries and inodes (moderately safe)
echo 2 > /proc/sys/vm/drop_caches
# Release all (use with caution in production)
echo 3 > /proc/sys/vm/drop_caches⚠️ This only provides temporary relief; the root cause remains swappiness or application memory management.
Swap is not the enemy, but frequent use is a performance disaster!
Automation script
#!/bin/bash
echo "=== Swap usage overview ==="
free -h
echo -e "
=== Swap activity (si/so) ==="
vmstat 1 3 | tail -1
echo -e "
=== Top 10 swap‑using processes ==="
for file in /proc/*/status; do
awk '/VmSwap|Name/{printf $2 " " $3}END{ print ""}' $file 2>/dev/null;
done | sort -k 2 -n -r | head -10
echo -e "
=== Current swappiness ==="
cat /proc/sys/vm/swappiness chmod +x swap-check.sh
./swap-check.shRegularly monitor your server’s state and adjust configurations to avoid performance bottlenecks.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Xiao Liu Lab
An operations lab passionate about server tinkering 🔬 Sharing automation scripts, high-availability architecture, alert optimization, and incident reviews. Using technology to reduce overtime and experience to avoid major pitfalls. Follow me for easier, more reliable operations!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
