ðŊ
What is Swap Memory?
Swap is a dedicated space on your storage device (SSD or HDD) that acts as a backup extension of your system's RAM. When your physical memory becomes full, Linux uses swap space to temporarily store inactive memory pages, ensuring that active processes continue running without crashing.
Physical RAM
32 GB
Fast Access (ns)
â
Swap Space
4 GB
Slow Access (Ξs/ms)
â
Storage
SSD/HDD
Permanent Storage
Memory Management Process
Normal Operation
RAM handles active processes
â
Memory Pressure
RAM approaches capacity
â
Swapping Out
Inactive pages â Swap
â
Swapping In
Needed pages â Swap
ðĄ Key Concept
Think of swap as a safety net for your system's memory management. While it's much slower than actual RAM due to disk I/O limitations, it prevents catastrophic system failures when memory demand exceeds available physical RAM.
ð
Understanding Your Swap Output
Your System Shows:
Swap:
4.0Gi
0B
4.0Gi
4.0 GB
Total Swap Space
Dedicated partition/file on your storage device
0 Bytes
Currently Used
No swap space is being utilized right now
4.0 GB
Available
All swap space is ready for use when needed
Why Zero Usage is Normal
Physical RAM: 32 GB total
Currently used: ~1 GB (972 MiB)
Free RAM: ~31 GB available
if (available_ram > required_memory) {
use_ram(); // Fast access
} else {
use_swap(); // Slower, but prevents crashes
}
âïļ
How Swap Memory Works
Performance Characteristics
Storage Type |
Access Time |
Relative Speed |
Best Use Case |
Physical RAM |
Nanoseconds |
1x (Baseline) |
Active processes & data |
SSD Swap |
Microseconds |
1,000x slower |
Modern systems, acceptable performance |
HDD Swap |
Milliseconds |
1,000,000x slower |
Emergency use only, noticeable slowdown |
When Swap Becomes Active
ðĨïļ
Virtual Machines
Running multiple VMs can quickly consume available RAM
ðĪ
AI/ML Workloads
Training models or large dataset processing
ðïļ
Database Operations
Large database caching and processing
ðŽ
Media Processing
Video editing, 3D rendering, image processing
Swappiness Configuration
cat /proc/sys/vm/swappiness
60
0 = Avoid swap except to prevent OOM
10 = Very low swap usage
60 = Default balanced approach
100 = Aggressive swapping
sudo sysctl vm.swappiness=10
echo "vm.swappiness=10" | sudo tee -a /etc/sysctl.conf
ðĄ Swappiness Recommendations
For systems with abundant RAM (16GB+): Set swappiness to 10-20
For systems with limited RAM (8GB or less): Keep default 60
For servers: Consider 1-10 for performance, 60 for safety
ð§
Optimal Swap Configuration
Swap Size Recommendations
System RAM |
Recommended Swap |
With Hibernation |
Reasoning |
2GB or less |
2x RAM |
3x RAM |
Need significant swap buffer |
4GB - 8GB |
1.5x RAM |
2x RAM |
Moderate swap for memory pressure |
16GB - 32GB |
2GB - 4GB |
Equal to RAM |
Safety net for edge cases |
64GB+ |
2GB - 8GB |
Equal to RAM |
Minimal swap for emergencies |
Creating and Managing Swap
free -h
swapon --show
cat /proc/swaps
sudo fallocate -l 4G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
sudo mkswap /dev/sdX2
sudo swapon /dev/sdX2
Performance Optimization
# NVMe SSD > SATA SSD > HDD
swapon -p 5 /path/to/fast/swap
swapon -p 1 /path/to/slow/swap
iostat -x 1 5 # Monitor disk I/O
sar -S 1 5 # Monitor swap activity
â ïļ Storage Placement
Always place swap on your fastest storage device. Using swap on a slow HDD while having a fast SSD available will severely impact performance when swapping occurs.
ð
Monitoring Swap Usage
Essential Monitoring Commands
free -h
total used free shared buff/cache available
Mem: 32Gi 972Mi 29Gi 123Mi 1.8Gi 30Gi
Swap: 4.0Gi 0B 4.0Gi
watch -n 1 'free -h'
cat /proc/swaps
Filename Type Size Used Priority
/swapfile file 4194300 0 -2
ps aux --sort=-%mem | head -10
top -o %MEM
htop
Advanced Monitoring
sar -S 1 5
iostat -x 1 5
vmstat 1 5
for file in /proc/*/status; do
awk '/VmSwap|Name/{printf $1 " " $2 " " $3}END{ print ""}' $file
done | sort -k 3 -n -r | head
grep -E "SwapTotal|SwapFree|SwapCached" /proc/meminfo
Warning Signs to Watch For
25%+
Swap Usage
Investigate if swap usage consistently exceeds 25%
High
Swap I/O
Frequent swapping indicates memory pressure
Slow
System Response
Applications becoming unresponsive during normal operations
ð
Troubleshooting Common Issues
Problem: System Slowdown with Available RAM
free -h
cat /proc/sys/vm/swappiness
sudo sysctl vm.swappiness=10
sudo swapoff -a
sudo swapon -a
Problem: Out of Memory Despite Available Swap
ps aux --sort=-%mem | head -20
watch -n 5 'ps aux --sort=-%mem | head -10'
ulimit -a
cat /proc/sys/vm/overcommit_memory
Problem: Swap File Creation Fails
df -h
# For older systems, use dd instead:
sudo dd if=/dev/zero of=/swapfile bs=1G count=4
ls -la /swapfile
sudo chown root:root /swapfile
sudo chmod 600 /swapfile
Emergency Recovery
echo f | sudo tee /proc/sysrq-trigger
sudo pkill -f "process-name"
sudo kill -9 $(ps aux --sort=-%mem | head -2 | tail -1 | awk '{print $2}')
sudo fallocate -l 2G /tmp/emergency-swap
sudo mkswap /tmp/emergency-swap
sudo swapon /tmp/emergency-swap
â ïļ Emergency Procedures
Emergency swap creation in
/tmp
is temporary and may not persist across reboots. Always create proper swap configuration after resolving the immediate crisis.
ð
Summary & Best Practices
â
Your Current Setup
4GB swap with 32GB RAM - Excellent safety net configuration
ðŊ
Zero Usage Normal
Unused swap with abundant RAM indicates optimal performance
ðĄïļ
Safety Insurance
Swap provides protection against unexpected memory spikes
âĄ
Performance Ready
System operates at peak efficiency without disk I/O overhead
ðĄ Key Takeaways
Unused swap space is not wasted space - it's available capacity providing peace of mind and system stability when you need it most. Your current configuration shows a healthy system setup with adequate safety margins for memory management.