Master Linux swap memory management, understand why your system shows 0B swap usage, and learn optimal configuration strategies for different workloads and system specifications.
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.
The memory hierarchy: Physical RAM (32 GB, fast access in nanoseconds) ⇄ Swap Space (4 GB, slow access in microseconds/milliseconds) → Storage (SSD/HDD, permanent storage).
The memory management process flows through four stages:
- Normal Operation — RAM handles active processes
- Memory Pressure — RAM approaches capacity
- Swapping Out — Inactive pages → Swap
- Swapping In — Needed pages ← Swap
NOTE
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
# Your system memory breakdown:
Physical RAM: 32 GB total
Currently used: ~1 GB (972 MiB)
Free RAM: ~31 GB available
# Kernel logic:
if (available_ram > required_memory) {
use_ram(); // Fast access
} else {
use_swap(); // Slower, but prevents crashes
}
NOTE
With 32 GB of RAM and only ~1 GB in use, your system has no reason to use the slower swap space. This is optimal behavior — unused swap means your system is performing at peak efficiency.
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
# Check current swappiness setting
cat /proc/sys/vm/swappiness
60
# Swappiness values (0-100):
0 = Avoid swap except to prevent OOM
10 = Very low swap usage
60 = Default balanced approach
100 = Aggressive swapping
# Temporarily change swappiness
sudo sysctl vm.swappiness=10
# Permanently change (add to /etc/sysctl.conf)
echo "vm.swappiness=10" | sudo tee -a /etc/sysctl.conf
NOTE
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
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
# Check current swap status
free -h
swapon --show
cat /proc/swaps
# Create a swap file (4GB example)
sudo fallocate -l 4G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
# Make swap permanent (add to /etc/fstab)
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
# Create swap partition (use with caution)
sudo mkswap /dev/sdX2
sudo swapon /dev/sdX2
Performance Optimization
# Place swap on fastest storage
# NVMe SSD > SATA SSD > HDD
# Optimize swap priority (higher = preferred)
swapon -p 5 /path/to/fast/swap
swapon -p 1 /path/to/slow/swap
# Check swap performance
iostat -x 1 5 # Monitor disk I/O
sar -S 1 5 # Monitor swap activity
WARNING
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
# Real-time memory and swap monitoring
free -h
total used free shared buff/cache available
Mem: 32Gi 972Mi 29Gi 123Mi 1.8Gi 30Gi
Swap: 4.0Gi 0B 4.0Gi
# Continuous monitoring
watch -n 1 'free -h'
# Detailed swap information
cat /proc/swaps
Filename Type Size Used Priority
/swapfile file 4194300 0 -2
# Memory usage by process
ps aux --sort=-%mem | head -10
top -o %MEM
htop
Advanced Monitoring
# Monitor swap I/O activity
sar -S 1 5
iostat -x 1 5
vmstat 1 5
# Check processes using swap
for file in /proc/*/status; do
awk '/VmSwap|Name/{printf $1 " " $2 " " $3}END{ print ""}' $file
done | sort -k 3 -n -r | head
# System memory pressure indicators
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.
NOTE
Set up automated monitoring alerts for swap usage above 50% and consider it critical above 80%. Use tools like
nagios, zabbix, or prometheus for production systems.
Troubleshooting Common Issues
Problem: System Slowdown with Available RAM
# Check if swap is being used unnecessarily
free -h
cat /proc/sys/vm/swappiness
# Solution: Reduce swappiness
sudo sysctl vm.swappiness=10
# Clear swap if not needed
sudo swapoff -a
sudo swapon -a
Problem: Out of Memory Despite Available Swap
# Check for memory leaks
ps aux --sort=-%mem | head -20
# Monitor memory growth
watch -n 5 'ps aux --sort=-%mem | head -10'
# Check system limits
ulimit -a
cat /proc/sys/vm/overcommit_memory
Problem: Swap File Creation Fails
# Check available disk space
df -h
# Verify filesystem supports fallocate
# For older systems, use dd instead:
sudo dd if=/dev/zero of=/swapfile bs=1G count=4
# Check permissions and ownership
ls -la /swapfile
sudo chown root:root /swapfile
sudo chmod 600 /swapfile
Emergency Recovery
# System unresponsive due to memory pressure
# Try Magic SysRq keys (if enabled):
echo f | sudo tee /proc/sysrq-trigger
# Kill memory-heavy processes
sudo pkill -f "process-name"
sudo kill -9 $(ps aux --sort=-%mem | head -2 | tail -1 | awk '{print $2}')
# Add emergency swap quickly
sudo fallocate -l 2G /tmp/emergency-swap
sudo mkswap /tmp/emergency-swap
sudo swapon /tmp/emergency-swap
WARNING
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.
NOTE
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.
Related
Pooling VRAM Across Two GPUs →