Swap space is more than just a fallback for RAM; it’s a crucial performance tuning knob that can significantly impact how your Linux system handles memory pressure.

Let’s see swap in action. Imagine a server running a memory-hungry application, like a database.

# Initial state: low swap usage
free -h
#              total        used        free      shared  buff/cache   available
# Mem:          7.8Gi       3.2Gi       1.1Gi       150Mi       3.5Gi       4.1Gi
# Swap:         2.0Gi          0B       2.0Gi

# Simulate memory pressure by allocating a large chunk of memory
python -c 'import sys; sys.setrecursionlimit(1000000); [1] * (1024*1024*1024 * 2)' &

# Observe swap usage increasing
free -h
#              total        used        free      shared  buff/cache   available
# Mem:          7.8Gi       5.2Gi       700Mi       150Mi       1.9Gi       2.1Gi
# Swap:         2.0Gi       1.5Gi       500Mi

Here, the [1] * (1024*1024*1024 * 2) command attempts to allocate 2GB of memory. As RAM (7.8Gi total) fills up, the kernel starts moving less-used memory pages from RAM to swap space, freeing up RAM for the active process. You can see used swap jump from 0B to 1.5Gi.

Swap space can be implemented as a dedicated partition or a file.

Creating a Swap File:

  1. Allocate space: Use fallocate for efficiency.

    sudo fallocate -l 4G /swapfile
    

    This creates a 4GB file named /swapfile.

  2. Set permissions: Restrict access to root.

    sudo chmod 600 /swapfile
    

    This ensures only the root user can read or write to the swap file, preventing unauthorized access to sensitive memory contents.

  3. Format as swap:

    sudo mkswap /swapfile
    

    This sets up the file to be used as Linux swap space.

  4. Enable swap:

    sudo swapon /swapfile
    

    This immediately activates the swap file for use by the kernel.

  5. Make permanent: Add to /etc/fstab.

    echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
    

    This entry tells the system to mount /swapfile as swap space during every boot.

Tuning Swap Behavior:

The kernel’s swappiness parameter controls how aggressively it swaps out memory. It’s a value between 0 and 100.

  • vm.swappiness = 60 (default): The kernel balances between swapping out inactive pages and dropping filesystem cache. This is a good general-purpose setting.
  • vm.swappiness = 10: The kernel will try to keep data in RAM as much as possible, only swapping when absolutely necessary. This is beneficial for desktops or systems where interactive responsiveness is paramount.
    sudo sysctl vm.swappiness=10
    
  • vm.swappiness = 90: The kernel will aggressively swap out inactive memory pages, prioritizing keeping file cache in RAM. This can be useful for servers with very large amounts of RAM where swapping out infrequently used application data is acceptable.
    sudo sysctl vm.swappiness=90
    

To make swappiness changes permanent, add the following line to /etc/sysctl.conf:

vm.swappiness = 10

Then apply it with sudo sysctl -p.

Another important parameter is vfs_cache_pressure. This controls how aggressively the kernel reclaims memory used for dentry and inode caches.

  • vm.vfs_cache_pressure = 100 (default): The kernel reclaims dentry and inode caches at a "normal" rate.
  • vm.vfs_cache_pressure = 50: The kernel will be less aggressive about reclaiming these caches, meaning they’ll stay in memory longer. This can improve performance for I/O-intensive workloads by keeping frequently accessed file metadata readily available.
    sudo sysctl vm.vfs_cache_pressure=50
    
    Add vm.vfs_cache_pressure = 50 to /etc/sysctl.conf for persistence.

The system automatically manages swappiness based on available RAM. If you have a lot of RAM and don’t want the kernel to swap much, setting swappiness to a low value like 10 or 20 is effective. If you have limited RAM and want to avoid Out-Of-Memory (OOM) killer situations, a higher swappiness value might help, but it comes at the cost of potential performance degradation.

When you have very little free RAM and swap is heavily utilized, the system’s performance will degrade significantly. This is because disk I/O (where swap resides) is orders of magnitude slower than RAM access. The kernel will spend a lot of time moving pages between RAM and swap, leading to what’s commonly called "thrashing."

The swapiness parameter doesn’t directly tell the kernel when to swap, but rather how much it prefers to swap versus dropping filesystem cache. A lower swappiness value means the kernel prefers to drop cache pages before swapping application memory.

The next logical step is understanding how to monitor swap performance and identify when thrashing is occurring.

Want structured learning?

Take the full Linux & Systems Programming course →