Swap space acts as overflow memory. When the physical RAM is full, the Linux kernel moves the least-recently-used memory pages to swap on disk, freeing physical RAM for active processes. Without swap, when RAM is exhausted the Out of Memory (OOM) killer terminates processes — often the largest one, which is frequently your database or application server. Swap is not a substitute for adequate RAM, but it is an essential safety net and a tunable tool for controlling which processes stay in RAM and which can be swapped out.

On RHEL 9, swap can be configured as a dedicated disk partition (created during installation) or as a swap file on any filesystem. Swap files are far more flexible: they can be created on the fly, resized without repartitioning, and located on any filesystem that supports them (ext4, xfs). This guide covers both approaches, plus tuning the vm.swappiness and vm.vfs_cache_pressure kernel parameters, monitoring swap usage, and understanding when to add more RAM instead of more swap.

Prerequisites

  • RHEL 9 server with root or sudo access
  • Available disk space for swap (at least 2 GB recommended)

Step 1 — Check Current Swap Status

# List all swap devices and their sizes
swapon --show

# Summary with total RAM and swap
free -h

# More detail from /proc/swaps
cat /proc/swaps

If swapon --show returns no output, no swap is currently configured.

Step 2 — Create a Swap File

A swap file is the most flexible approach. Create a 4 GB swap file at /swapfile:

# Create a 4 GB file using fallocate (fast, pre-allocates all blocks)
fallocate -l 4G /swapfile

If your filesystem is XFS and fallocate produces an error (“fallocate failed: Operation not supported”), use dd instead:

dd if=/dev/zero of=/swapfile bs=1M count=4096 status=progress

Set the correct permissions. The swap file must be readable and writable only by root:

chmod 600 /swapfile
ls -lh /swapfile

Format it as swap space:

mkswap /swapfile

Activate it immediately:

swapon /swapfile

Verify it is active:

swapon --show
free -h

Step 3 — Make the Swap File Permanent

Add the swap file to /etc/fstab so it activates automatically on reboot:

echo '/swapfile none swap sw 0 0' >> /etc/fstab

Test the fstab entry without rebooting:

mount -a
swapon --show

Step 4 — Create a Swap Partition (Alternative)

If you have an unpartitioned disk or LVM free space, you can create a dedicated swap partition. This is marginally faster than a swap file because there is no filesystem overhead:

# List disks and partitions
lsblk

# Create a swap partition with fdisk (replace /dev/sdb with your disk)
fdisk /dev/sdb
# In fdisk: n (new partition), accept defaults for full disk, then t (change type), 82 (Linux swap)

# Or with LVM
lvcreate -L 4G -n swap vg0
mkswap /dev/vg0/swap
swapon /dev/vg0/swap

# Add to fstab using UUID for stability
blkid /dev/vg0/swap
echo 'UUID=YOUR-UUID none swap sw 0 0' >> /etc/fstab

Step 5 — Tune vm.swappiness

The vm.swappiness kernel parameter controls how aggressively the kernel moves memory pages to swap. It ranges from 0 to 200:

  • 0 — avoid swapping unless absolutely necessary (RAM is completely exhausted). Suitable for database servers where RAM access speed is critical.
  • 10 — low swappiness, good for most production servers. The kernel will only swap when RAM usage is high.
  • 60 — the default. Balanced for general workloads.
  • 100 — equal preference for RAM and swap. Not recommended for production.
# Check current value
cat /proc/sys/vm/swappiness

# Set it temporarily (survives until reboot)
sysctl vm.swappiness=10

# Make it permanent
echo 'vm.swappiness=10' >> /etc/sysctl.d/99-swap.conf
sysctl -p /etc/sysctl.d/99-swap.conf

Step 6 — Tune vm.vfs_cache_pressure

This parameter controls how aggressively the kernel reclaims memory used for filesystem caches (dentries and inodes). The default is 100. A lower value like 50 makes the kernel less aggressive about reclaiming cache memory, which is good for servers with frequent filesystem operations:

echo 'vm.vfs_cache_pressure=50' >> /etc/sysctl.d/99-swap.conf
sysctl -p /etc/sysctl.d/99-swap.conf

Step 7 — Monitor Swap Usage in Real Time

# Overall swap usage
free -h

# Swap usage per process (top 10 by swap)
for proc in /proc/[0-9]*/status; do
    pid=$(echo "$proc" | cut -d/ -f3)
    name=$(grep Name "$proc" | awk '{print $2}')
    vmswap=$(grep VmSwap "$proc" 2>/dev/null | awk '{print $2}')
    [ -n "$vmswap" ] && [ "$vmswap" -gt 0 ] && echo "$vmswap kB - $pid ($name)"
done | sort -rn | head -10

# vmstat to see swap in/out activity over time
vmstat 5 10

# sar for historical swap data (requires sysstat package)
sar -W 1 5

Step 8 — Resize the Swap File

To increase swap size, create an additional swap file rather than resizing the existing one (you cannot resize an active swap file):

# Add a second swap file
fallocate -l 4G /swapfile2
chmod 600 /swapfile2
mkswap /swapfile2
swapon /swapfile2
echo '/swapfile2 none swap sw 0 0' >> /etc/fstab

To remove a swap file:

swapoff /swapfile2
rm /swapfile2
# Remove the line from /etc/fstab

Swap Sizing Guidelines

Red Hat recommendations for RHEL 9:

  • RAM < 2 GB: swap = 2x RAM
  • RAM 2–8 GB: swap = equal to RAM
  • RAM 8–64 GB: swap = at minimum 4 GB
  • RAM > 64 GB: swap configured based on workload requirements; often 4–8 GB for safety
  • Hibernate support required: swap must be at least equal to RAM size

Verification Checklist

# Active swap devices
swapon --show

# Persistent fstab entry
grep swap /etc/fstab

# Current sysctl values
sysctl vm.swappiness vm.vfs_cache_pressure

# RAM and swap summary
free -h

Troubleshooting

  • fallocate failed: Operation not supported — your filesystem (XFS or BTRFS) does not support fallocate for swap. Use dd instead as shown in Step 2.
  • swapon: /swapfile: insecure permissions 0644, 0640 suggested — run chmod 600 /swapfile. Swap files must not be world-readable as they may contain sensitive memory pages.
  • Swap is full and system is very slow — the server needs more RAM. Swap on spinning disk is 100x slower than RAM. Check which processes are using the most swap and consider restarting them, or scale up the server.

Conclusion

Your RHEL 9 server now has swap space configured, made persistent in /etc/fstab, and tuned with appropriate vm.swappiness and vm.vfs_cache_pressure values for your workload. You can monitor swap usage per process and know how to add additional swap files without downtime.

Next steps: How to Monitor System Resources with htop, top, and vmstat on RHEL 9, How to Configure Automatic Security Updates on RHEL 9, and How to Manage LVM Storage on RHEL 9.