How big should swap be, and how to add it: a swapfile walkthrough (Rocky 9)
When to use it
Section titled “When to use it”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.
Check the current state
Section titled “Check the current state”cat /proc/swapsswapon --showFilename Type Size Used Priority/dev/zram0 partition 24639296 22642820 32767/dev/vdc partition 1048572 390964 1Output means swap is already enabled; no output means none is active. free -h works too, but minimal systems may not have procps installed.
How big
Section titled “How big”| RAM | Suggested swap |
|---|---|
| ≤ 2 GB | 2× RAM |
| 2–8 GB | Equal to RAM |
| 8–64 GB | At least 4 GB |
| > 64 GB | Workload-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.
Create the swapfile
Section titled “Create the swapfile”sudo fallocate -l 4G /swapfilesudo chmod 600 /swapfilels -l /swapfilesudo mkswap /swapfile-rw------- 1 root root 4294967296 ... /swapfileSetting 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.
Enable and persist
Section titled “Enable and persist”sudo swapon /swapfileswapon --showecho '/swapfile none swap defaults 0 0' | sudo tee -a /etc/fstabOnly when swapon --show lists a new /swapfile entry is it active; the /etc/fstab line keeps it enabled after a reboot.
Verify
Section titled “Verify”swapon --showcat /proc/swapssudo swapoff /swapfile && swapon --show # confirm it can be disabled safelysudo swapon /swapfileContainer limitation: inside a
rockylinux:9containerfallocate,chmodandmkswapall succeed, butswaponreturnsOperation 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.
Common traps
Section titled “Common traps”fallocateworks on XFS, but on filesystems without preallocation support (some network filesystems) create the file withddinstead;swappinessis 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.
Practice: give a 4 GB host 4 GB of swap
Section titled “Practice: give a 4 GB host 4 GB of swap”Prerequisite: Rocky 9 with root or sudo and at least 5 GB free on the root filesystem.
sudo fallocate -l 4G /swapfilesudo chmod 600 /swapfilesudo mkswap /swapfilesudo swapon /swapfileecho '/swapfile none swap defaults 0 0' | sudo tee -a /etc/fstabChecks:
swapon --show→ a/swapfileentry of about 4Gcat /proc/swaps | wc -l→ one line more than before- After
sudo reboot,swapon --show→ still there (fstab works) sudo swapoff /swapfile && sudo swapon /swapfile→ disables and re-enables cleanly
Next steps
Section titled “Next steps”- Growing storage (page pending):
/en/storage-lvm/ - Post-install checks: First-hour checks
- Performance debugging: Performance tuning