Many Linux administrators face this scenario: your monitoring system keeps sending swap usage alerts even when RAM becomes available. The swap space remains stubbornly allocated, triggering hourly notifications that clutter your inbox while providing no actionable information.
Linux kernel's swappiness behavior keeps swapped-out pages in disk storage until absolutely necessary to move them back to RAM. This conservative approach prevents unnecessary disk I/O but leads to our alert problem.
# Check current swappiness value (default is usually 60)
cat /proc/sys/vm/swappiness
# View current swap usage
free -h
swapon --show
Here are three effective methods to free swap without system restart:
Method 1: The Systematic Approach
This method safely moves pages back to memory when possible:
# Make kernel attempt to swap in pages
sudo swapoff -a && sudo swapon -a
Note: This briefly disables all swap, so ensure you have sufficient free RAM.
Method 2: Targeted Page Clearing
For more granular control, use this advanced technique:
# Find processes using swap
sudo smem -s swap -r | head
# Then clear page caches (as root):
echo 3 > /proc/sys/vm/drop_caches
Method 3: Swappiness Adjustment
Temporarily reduce swappiness to encourage pages back to RAM:
# Set swappiness to 10 temporarily
sudo sysctl vm.swappiness=10
# Let it work for a while, then check swap
sleep 300; free -h
Create a cron job to prevent alerts during low-usage periods:
# In /etc/cron.hourly/swap_cleaner
#!/bin/bash
[ $(free | awk '/Swap:/ {print $3}') -eq 0 ] && exit 0
[ $(free | awk '/Mem:/ {print $7}') -gt 2097152 ] && swapoff -a && swapon -a
Sometimes the better solution is tuning your alerts:
# Example Nagios check that considers free memory
command[check_swap]=/usr/lib/nagios/plugins/check_swap -w 50% -c 20% -K
- Always maintain some swap for emergency situations
- Test these commands in non-production first
- Consider adding buffer to your alert thresholds
- Document any changes to swappiness values
Many Linux admins encounter situations where swap remains fully utilized even when system RAM becomes available. This triggers annoying hourly alerts while the kernel fails to automatically rebalance memory allocation. The issue typically occurs when:
- Applications with memory leaks were previously swapped out
- Kernel vm.swappiness settings are too aggressive
- Long-running processes maintain swapped-out pages unnecessarily
First verify your current memory state:
free -h
total used free shared buff/cache available
Mem: 15G 6.2G 7.1G 456M 2.3G 8.2G
Swap: 2.0G 2.0G 12K
Check which processes are using swap:
sudo smem -t -s swap
PID User Command Swap USS PSS RSS
4729 mysql /usr/sbin/mysqld 1.2G 64.3M 65.1M 784.2M
5821 redis /usr/bin/redis-server 512M 12.1M 12.8M 89.4M
Method 1: Drop Page Cache (Safest approach)
sudo sync; echo 3 | sudo tee /proc/sys/vm/drop_caches
Method 2: Targeted Process Restart
# Identify top swap consumer
sudo swapon --show
sudo swapoff -a && sudo swapon -a
Method 3: Adjust Swappiness Dynamically
sudo sysctl vm.swappiness=10
Create a cron job to prevent future alerts:
#!/bin/bash
SWAP_THRESHOLD=90
CURRENT=$(free | awk '/Swap/{printf "%.0f", $3/$2*100}')
if [ "$CURRENT" -ge "$SWAP_THRESHOLD" ]; then
echo "Clearing swap at ${CURRENT}% usage"
sudo swapoff -a && sudo swapon -a
fi
Make it executable and add to cron:
chmod +x /usr/local/bin/swap_monitor.sh
(crontab -l ; echo "*/15 * * * * /usr/local/bin/swap_monitor.sh") | crontab -
For extreme cases where processes won't release swap, use this nuclear option:
# Warning: This will OOM kill processes
echo 1 | sudo tee /proc/sys/vm/oom_kill_allocating_task
sudo sysctl vm.overcommit_memory=2
Consider adding these to /etc/sysctl.conf for permanent changes:
vm.swappiness = 10
vm.vfs_cache_pressure = 50