Linux Swap Optimization Guide: Resolving 100% Swap Usage

When a Linux server shows 100% swap usage despite having available RAM, it can significantly impact system performance. This guide explains why this happens and provides step-by-step instructions to resolve it.

Understanding the Problem

A fully utilized swap partition with available RAM typically occurs because:

  1. The system experienced a temporary memory pressure event
  2. Data was moved to swap during this event
  3. Linux doesn't automatically move this data back to RAM, even when memory becomes available

This behavior is by design but can lead to performance degradation as swap is much slower than RAM.

Diagnosis

First, verify the system state:

# Check memory and swap usage
free -h

# Identify top memory-consuming processes
ps aux --sort=-%mem | head -10

# View detailed memory information
lsmem

Step-by-Step Solution

1. Immediate Fix: Reclaim Swap Space

# Temporarily reduce swap aggressiveness
sudo sysctl vm.swappiness=5

# Move data from swap back to RAM
sudo swapoff -a && sudo swapon -a

Note: The swapoff -a command duration depends on swap size and system load. For a 4GB fully-utilized swap, expect 1-2 minutes of completion time.

2. Permanent Solution: Optimize Memory Management

# Check if parameters already exist
grep -E "vm.swappiness|vm.min_free_kbytes" /etc/sysctl.conf

# Set permanent lower swappiness
echo 'vm.swappiness=10' | sudo tee -a /etc/sysctl.conf
sudo sysctl -p

# Ensure system always keeps some RAM available
echo 'vm.min_free_kbytes=1048576' | sudo tee -a /etc/sysctl.conf  # 1GB minimum free
sudo sysctl -p

3. Verify the Changes

# Confirm swap is now available
free -h

# Verify sysctl parameters
sysctl vm.swappiness
sysctl vm.min_free_kbytes

Understanding the Parameters

vm.swappiness

This kernel parameter controls how aggressively Linux moves processes from RAM to swap:

  • Default: Usually 30-60
  • Recommended for servers: 10-20
  • Value range: 0-100 (lower values reduce swap usage)

Despite its name, "vm" refers to Virtual Memory, not Virtual Machines.

vm.min_free_kbytes

Specifies the minimum amount of memory to keep free:

  • Default: Varies by system memory size
  • Recommended for servers: 1048576 (1GB) for systems with >16GB RAM
  • Purpose: Prevents memory exhaustion and ensures critical kernel operations have memory

When to Apply This Solution

This approach is particularly effective when:

  1. Swap usage is high despite sufficient available RAM
  2. System has database workloads (MySQL/MariaDB, PostgreSQL, etc.)
  3. Performance is degraded due to excessive swapping
  4. Server has ample RAM (>16GB)

Additional Considerations

For database servers, consider reviewing database configuration parameters related to memory usage. For MySQL/MariaDB, key parameters include:

  • innodb_buffer_pool_size
  • tmp_table_size
  • max_heap_table_size
  • join_buffer_size

Safety Considerations

These operations carry minimal risk when:

  • The system has sufficient free RAM (more than swap usage)
  • The commands are executed with proper permissions
  • No critical system changes occur simultaneously

The process requires no service restarts or system reboots.

Read more