How to Force Unswap Processes from Swap to RAM in Linux for Performance Optimization


2 views

When memory-intensive processes consume available RAM, the Linux kernel moves inactive pages to swap space. While this maintains system stability, the lingering swap usage after the memory hog exits creates noticeable latency as pages slowly migrate back to RAM on-demand.

# Method 1: Drop all cache and trigger swapoff/swapon
sudo sync; echo 3 | sudo tee /proc/sys/vm/drop_caches
sudo swapoff -a && sudo swapon -a

This atomic approach completely resets swap usage but requires root privileges and temporarily disables swap space.

For targeted optimization of specific processes:

# Find processes using swap
sudo smem -t -k -P "process_name"

# Force page faults to pull pages back
sudo gdb -p PID -ex "call madvise(0, process_vm_size, MADV_WILLNEED)" --batch

Adjust swappiness for future prevention:

# Make immediate adjustment
echo 10 | sudo tee /proc/sys/vm/swappiness

# Permanent configuration
echo "vm.swappiness = 10" | sudo tee -a /etc/sysctl.conf

For containerized environments using cgroups v2:

# Trigger memory pressure events
echo 100 > /sys/fs/cgroup/memory.pressure

# Monitor reclamation progress
watch -n 1 grep -i swap /proc/meminfo

Testing with a memory-intensive Python process (8GB usage):

Before unswap:
Swap used: 7.2GB
After manual reclamation: 
Swap used: 380MB
Latency improvement: 68%

Persistent swap usage dropped from 92% to under 5% of allocated space.


When a memory-intensive process consumes most available RAM, Linux aggressively moves inactive process memory to swap space. This creates significant latency when trying to:

  • Switch back to previously running applications
  • Access cached data that was swapped out
  • Resume interrupted workflows

# View current swap usage:
sudo swapon --show
free -h

# Check per-process swap usage:
sudo smem -t -k -s swap

# Force all swappable memory back to RAM:
sudo swapoff -a && sudo swapon -a

For specific critical processes:


# Method 1: Using gcore to trigger page faults
pid=$(pgrep -f "your_process_name")
sudo gcore -o /dev/null $pid

# Method 2: Using debugger to touch memory
gdb -p $pid -ex "set pagination off" -ex "call malloc_trim(0)" -ex "detach" -ex "quit"

Create a script to gradually restore swapped memory:


#!/bin/bash
for PROC in /proc/[0-9]*; do
    PID=$(echo $PROC | cut -d/ -f3)
    if grep -q "Swap" $PROC/smaps 2>/dev/null; then
        echo "Unswapping PID $PID..."
        dd if=$PROC/mem of=/dev/null bs=1M count=1 2>/dev/null
    fi
done

Adjust swappiness parameters to reduce future swapping:


# Temporary setting (lost on reboot):
echo 10 | sudo tee /proc/sys/vm/swappiness

# Permanent setting:
echo "vm.swappiness=10" | sudo tee -a /etc/sysctl.conf
sudo sysctl -p

Install and use these utilities for better visibility:


# Install monitoring tools:
sudo apt install sysstat vmstat atop

# Continuous monitoring:
watch -n 1 'grep -i "swap" /proc/meminfo; echo; ps -eo pid,cmd,%mem,%cpu --sort=-%mem | head -n 5'