Managing swap space on RHEL and CentOS
Swap space is disk-backed storage that the Linux kernel uses for memory pages when physical RAM is under pressure. It acts as a safety valve, but sustained paging can make applications slow and may still end in an out-of-memory kill if reclaim fails.
A swap file is convenient for testing and modest workloads. A dedicated swap partition is more predictable on busy servers. Neither replaces sufficient RAM, and neither should be resized without checking free disk space, memory demand and the host’s failure behaviour.
RHEL, CentOS Stream and related enterprise distributions expose the same core tools: free, swapon, mkswap, fstab and sysctl. The examples below suit administrators managing local hosts, virtual machines and Australian cloud instances.
How swap fits into enterprise memory management
Swap stores anonymous memory pages that the kernel has moved out of RAM. It gives a process more virtual address space and delays an out-of-memory (OOM) kill when an application or service briefly needs more memory than is available. On RHEL and CentOS, the kernel decides which pages are eligible.
Swap is a pressure absorber, not extra processing power. Heavy, continuous paging increases latency and I/O, while a little swap usage can be harmless. Diagnose memory pressure with paging rates and OOM events rather than treating the swap percentage as a fault by itself.
Inspecting the current swap layout
Start with free -h, swapon --show, lsblk -f and cat /proc/swaps. The Linux commands reference provides broader command syntax and examples for each check.
Memory and device snapshot
free -hshows total, used and available RAM plus swap totals.swapon --showreports each active area, size, priority and path.lsblk -fidentifies disks, mount points, filesystem types and swap labels.
Pressure indicators
vmstat 1exposes active swap-in and swap-out activity undersiandso.sar -Wprovides historical paging statistics where the sysstat package is installed.dmesg -Tcan reveal recent OOM killer messages after a memory incident.
These two lists separate configuration from workload behaviour. A configured device may be idle, while a nearly full device can still be acceptable if paging has stopped and the OOM killer has not fired.
Record the output before any change so the baseline includes capacity, priority and filesystem location.
Adding swap safely
Choose a path with ample free space, ideally on a separate filesystem or data disk rather than a nearly full root volume. The following creates a 4 GiB swap file; adjust the count to the approved size.
sudo dd if=/dev/zero of=/swapfile bs=1M count=4096 status=progress
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
After activation, confirm /swapfile appears in swapon --show and that free -h reflects the added capacity. The dd operation writes the full file, so allow enough time and I/O on slow storage.
For a dedicated partition, create it with parted or fdisk, initialise it with mkswap /dev/sdX1, then activate it with swapon /dev/sdX1. Replace sdX with the correct disk identifier; never run mkswap against the wrong partition.
Making swap persistent at boot
Add /swapfile none swap sw 0 0 to /etc/fstab, or use the partition UUID for a dedicated area. Then run sudo systemctl daemon-reload and sudo swapon -a, and verify the result with swapon --show before rebooting.
Document why the change was made, its owner, capacity and rollback steps in the change record. The linked project planning guidance can help keep that work visible across shifts and support teams.
Tuning swap behaviour
vm.swappiness controls how eagerly the kernel chooses swap over page cache. Its range is 0 to 100; RHEL commonly uses 60 by default, but confirm the live value with sysctl vm.swappiness. Latency-sensitive servers often use 10, although the right setting depends on memory pressure.
Set a temporary test value with sudo sysctl -w vm.swappiness=10. To persist it, write vm.swappiness=10 into a file under /etc/sysctl.d/, then apply it with sudo sysctl --system. Avoid copying low values without measuring paging and application latency.
Swap priorities can reserve lower-priority areas for emergencies, and RHEL’s zram option provides compressed in-memory swap. Priorities alter ordering rather than create capacity; zram consumes RAM, so neither is a substitute for capacity planning.
Maintaining swap across Australian infrastructure
Australian environments may combine AWS workloads in the Sydney and Melbourne regions with on-premises servers or colocation in Brisbane and Perth. Local providers such as NEXTDC support that mixed estate, so swap settings should be included in host images and failover procedures rather than applied only to one data centre.
Swap can contain remnants of customer data. For workloads covered by the Privacy Act 1988 or aligned with ASD and ACSC security practices, restrict file access, encrypt the underlying storage and avoid treating swap as a reliable sanitisation method. Swap is not encrypted automatically.
Build checks for active swap, paging rate, disk capacity and OOM events into routine monitoring. Administrators can also use the site’s swap interview questions to refresh the reasoning behind these controls. The next controlled action is to schedule a 30-minute test on a non-production RHEL or CentOS host, add a 1 GiB swap file, and save the before-and-after swapon --show output in the change record.