Seeing swap usage despite abundant free RAM is actually a deliberate Linux kernel behavior called "swapiness." The kernel proactively moves rarely-used memory pages to swap even when RAM isn't full, maintaining performance headroom.
Modern Linux kernels (especially 4.x+) implement these memory management strategies:
# Check current swappiness value
cat /proc/sys/vm/swappiness
# Typical output (default=60):
60
The vm.swappiness parameter (0-100) controls this behavior. Higher values make the kernel more aggressive about swapping idle pages.
Monitor these metrics to determine if swap usage is problematic:
# Check memory pressure
vmstat 1 5
# Check page in/out rates
sar -B 1 5
Red flags include:
- High si/so values in vmstat
- pgscank/pgscand in sar showing frequent scanning
- Major page faults increasing
For database servers or latency-sensitive apps, consider tuning:
# Temporarily set swappiness lower
sudo sysctl vm.swappiness=10
# Make permanent (in /etc/sysctl.conf):
vm.swappiness = 10
vm.vfs_cache_pressure = 50
For MySQL servers with large buffers, we typically:
# Add to /etc/sysctl.conf
vm.swappiness = 1
vm.dirty_ratio = 80
vm.dirty_background_ratio = 5
This configuration keeps database working sets firmly in RAM while still allowing emergency swapping if absolutely needed.
For deeper analysis, use performance tools:
# Install perf-tools
sudo apt install linux-tools-common linux-tools-generic
# Track page cache hits
sudo perf stat -e page-faults,dTLB-load-misses sleep 10
Remember: Some swap usage isn't inherently bad. The kernel uses it strategically to maintain overall system performance.
What you're observing is actually expected behavior in Linux systems. The kernel's memory management prefers to keep some data in swap even when physical memory is available. This design choice stems from several technical considerations:
# Check current swap usage:
$ free -h
total used free shared buff/cache available
Mem: 62G 20G 39G 456M 2.5G 41G
Swap: 4G 1.0G 3.0G
# View swappiness value:
$ cat /proc/sys/vm/swappiness
60
Swappiness Parameter: Linux uses a tunable parameter called vm.swappiness (default 60) that controls the kernel's preference for swap usage versus dropping pages from the page cache.
Inactive Memory Pages: The kernel may move rarely accessed memory pages to swap to make room for disk caching, which often provides better overall system performance.
# View inactive/active memory breakdown:
$ cat /proc/meminfo | grep -i active
Active: 12345678 kB
Inactive: 9876543 kB
While your current situation is normal, these scenarios would warrant investigation:
- Swap usage growing continuously despite free memory
- Noticeable performance degradation
- High si/so values in
vmstat 1
output (indicating frequent swap in/out operations)
If you want to reduce swap usage (though not necessarily recommended):
# Temporarily adjust swappiness (lower value = less swap usage)
sudo sysctl vm.swappiness=10
# Make permanent:
echo "vm.swappiness = 10" >> /etc/sysctl.conf
# To completely disable swapping (not recommended for production):
sudo swapoff -a
For mission-critical applications that must avoid swap entirely, consider using:
# Use cgroups v2 to disable swap for specific processes:
echo "+memory" > /sys/fs/cgroup/cgroup.subtree_control
mkdir /sys/fs/cgroup/mycgroup
echo "memory.swap.max = 0" > /sys/fs/cgroup/mycgroup/memory.swap.max
echo <pid> > /sys/fs/cgroup/mycgroup/cgroup.procs
These commands help analyze memory/swap patterns:
# Comprehensive memory overview:
$ vmstat -s
# Identify processes using swap:
$ sudo smem -t -k -s swap
# Real-time monitoring:
$ sudo atop -m