How to Set Up a Swap Partition

    Configure swap space for improved memory management

    Overview

    Swap space is a portion of disk storage used as virtual memory when your system's physical RAM is fully utilized. It provides a safety net that prevents out-of-memory (OOM) errors and system crashes when memory-intensive applications temporarily exceed available RAM.

    Cloud VPS instances typically do not come with swap pre-configured, giving you full control over whether and how much swap space to allocate based on your workload requirements.

    Swap File vs Swap Partition

    This guide uses swap files rather than swap partitions. Swap files are more flexible—you can resize them easily without repartitioning your disk. Performance is virtually identical on modern Linux kernels.

    When to Use Swap

    Good Use Cases

    • • Systems with limited RAM (1-2GB)
    • • Applications with variable memory usage
    • • Preventing OOM killer from terminating processes
    • • Hibernation support (rare on servers)
    • • Running memory-intensive tasks occasionally

    Considerations

    • • Disk I/O is much slower than RAM
    • • Heavy swapping indicates need for more RAM
    • • SSD wear from frequent swap writes
    • • Database servers often perform better without swap

    Create a Swap File

    Step 1: Check Existing Swap

    First, verify if swap is already configured on your system:

    sudo swapon --show
    free -h

    If no output from swapon --show, no swap is currently active.

    Step 2: Check Available Disk Space

    Ensure you have enough disk space for the swap file:

    df -h /

    Make sure you have at least a few GB more than your intended swap size available.

    Step 3: Create the Swap File

    Use fallocate or dd to create the swap file. This example creates a 2GB swap file:

    # Method 1: Using fallocate (faster)
    sudo fallocate -l 2G /swapfile
    
    # Method 2: Using dd (works on all filesystems)
    sudo dd if=/dev/zero of=/swapfile bs=1M count=2048 status=progress

    Note: fallocate may not work on all filesystems (e.g., XFS with certain configurations, Btrfs). Use dd if you encounter errors.

    Step 4: Set Correct Permissions

    The swap file must only be accessible by root for security:

    sudo chmod 600 /swapfile
    ls -lh /swapfile

    You should see -rw------- 1 root root in the output.

    Step 5: Format as Swap

    Mark the file as swap space:

    sudo mkswap /swapfile

    Enable Swap

    Activate the swap file immediately:

    sudo swapon /swapfile

    Verify it's active:

    sudo swapon --show
    free -h

    You should now see your swap file listed with the correct size.

    Make Swap Persistent

    Add the swap file to /etc/fstab so it's enabled on boot:

    # Backup fstab first
    sudo cp /etc/fstab /etc/fstab.backup
    
    # Add swap entry
    echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

    Verify the entry was added:

    cat /etc/fstab

    You should see a line like: /swapfile none swap sw 0 0

    Tune Swappiness

    The swappiness parameter controls how aggressively the kernel swaps memory pages. Values range from 0 to 100:

    • 0: Swap only when absolutely necessary (avoid swapping)
    • 10-30: Recommended for servers (minimize swapping)
    • 60: Default for most Linux distributions
    • 100: Swap aggressively

    Check current value:

    cat /proc/sys/vm/swappiness

    Set temporarily (until reboot):

    sudo sysctl vm.swappiness=10

    Make permanent:

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

    Recommended for Cloud VPS

    A swappiness value of 10 is generally recommended for servers. This ensures the system prefers using RAM but can still use swap when memory pressure is high, preventing OOM conditions.

    Verify Configuration

    Run these commands to confirm everything is configured correctly:

    # Show swap status
    sudo swapon --show
    
    # Show memory and swap usage
    free -h
    
    # Verify fstab entry
    grep swap /etc/fstab
    
    # Check swappiness setting
    cat /proc/sys/vm/swappiness

    Expected output for a 2GB swap file:

    NAME      TYPE SIZE USED PRIO
    /swapfile file   2G   0B   -2

    Remove Swap (If Needed)

    If you need to remove swap later:

    # Disable swap
    sudo swapoff /swapfile
    
    # Remove from fstab (edit manually or use sed)
    sudo sed -i '/\/swapfile/d' /etc/fstab
    
    # Delete the swap file
    sudo rm /swapfile
    
    # Verify
    free -h

    Best Practices

    Monitor Swap Usage

    Regularly monitor swap usage with free -h or vmstat 1. Consistent high swap usage indicates you may need more RAM.

    Don't Over-Allocate

    More swap isn't always better. Excessive swapping severely impacts performance. If you're frequently using swap, consider upgrading your instance.

    Use SSD Storage

    RamNode Cloud VPS uses fast SSD/NVMe storage, making swap more viable than on traditional spinning disks. However, RAM is still significantly faster.

    Database Servers

    For database servers (MySQL, PostgreSQL), some DBAs prefer minimal or no swap. The database should manage its own memory. Monitor carefully in production.

    Need More Memory?

    If your workload consistently requires more memory than your current instance provides, consider resizing your instance for better performance than relying heavily on swap.