Skip to content
Search Rocky Linux

    Browse this documentation

    How big should swap be, and how to add it: a swapfile walkthrough (Rocky 9)

    Processes get killed by the OOM killer, or a small-memory host needs a cushion. Swap is not “the more the better”: too small and it does nothing, too large and memory pressure turns into disk thrashing. This page gives the sizing rule first, then a copy-paste swapfile procedure.

    Terminal window
    cat /proc/swaps
    swapon --show
    Filename Type Size Used Priority
    /dev/zram0 partition 24639296 22642820 32767
    /dev/vdc partition 1048572 390964 1

    Output means swap is already enabled; no output means none is active. free -h works too, but minimal systems may not have procps installed.

    RAMSuggested swap
    ≤ 2 GB2× RAM
    2–8 GBEqual to RAM
    8–64 GBAt least 4 GB
    > 64 GBWorkload-dependent, at least 4 GB

    The table follows Red Hat’s recommended ranges (RHEL 9 storage docs, checked 2026-10-18). It is a starting point, not a law: databases, container hosts and hosts with elastic memory needs should size from real workload data.

    Terminal window
    sudo fallocate -l 4G /swapfile
    sudo chmod 600 /swapfile
    ls -l /swapfile
    sudo mkswap /swapfile
    -rw------- 1 root root 4294967296 ... /swapfile
    Setting up swapspace version 1, size = 4 GiB (4294963200 bytes)
    no label, UUID=...

    chmod 600 is not optional: with 644 permissions mkswap warns and swapon refuses the file — a world-readable swapfile leaks memory contents.

    Terminal window
    sudo swapon /swapfile
    swapon --show
    echo '/swapfile none swap defaults 0 0' | sudo tee -a /etc/fstab

    Only when swapon --show lists a new /swapfile entry is it active; the /etc/fstab line keeps it enabled after a reboot.

    Terminal window
    swapon --show
    cat /proc/swaps
    sudo swapoff /swapfile && swapon --show # confirm it can be disabled safely
    sudo swapon /swapfile

    Container limitation: inside a rockylinux:9 container fallocate, chmod and mkswap all succeed, but swapon returns Operation not permitted (the container lacks the privilege). So the “enabled” step must be verified on a real system; the container only proves the file-creation steps.

    • fallocate works on XFS, but on filesystems without preallocation support (some network filesystems) create the file with dd instead;
    • swappiness is not “higher is better”: database workloads usually lower it (e.g. 10) so the kernel prefers RAM;
    • zram and swapfile are not either/or: small hosts can prefer zram with a swapfile as a backstop, but both consume resources and belong in capacity planning.

    Prerequisite: Rocky 9 with root or sudo and at least 5 GB free on the root filesystem.

    Terminal window
    sudo fallocate -l 4G /swapfile
    sudo chmod 600 /swapfile
    sudo mkswap /swapfile
    sudo swapon /swapfile
    echo '/swapfile none swap defaults 0 0' | sudo tee -a /etc/fstab

    Checks:

    1. swapon --show → a /swapfile entry of about 4G
    2. cat /proc/swaps | wc -l → one line more than before
    3. After sudo reboot, swapon --show → still there (fstab works)
    4. sudo swapoff /swapfile && sudo swapon /swapfile → disables and re-enables cleanly