Candid photograph of a Linux server terminal with a soft olive-green glow against a dark slate background, conveying a calm technical atmosphere.

Step-by-step guides for system administrators — covering command-line basics, web server setup, and preparation material for technical interviews.

Browse Tutorials

Managing swap space with fallocate and swapon on CentOS

Swap gives CentOS room to move cold memory pages out of RAM and into secondary storage. It does not add equivalent memory: a page read from swap is much slower than one from RAM, so sustained swapping usually signals pressure on memory limits.

Creating a file-based swap area is practical when a disk cannot be repartitioned. fallocate reserves blocks, mkswap prepares the area, and swapon activates it. The same procedure works for a legacy CentOS 7 host or a current CentOS Stream release, provided the filesystem supports the operation.

Runbook wording should use Australian English, including “initialise” and “authorised”. Record the release, filesystem, available capacity, intended swap size and rollback steps; these details make a later maintenance check much easier.

How CentOS manages virtual memory

The kernel weighs reclaim pressure against vm.swappiness and may use swap before RAM appears completely full. Swap devices can also have different priorities, with higher-priority devices used first. This makes swap useful as a pressure valve, not a reason to configure a large, idle file.

Approach Allocation and activation Operational fit Main caution
fallocate Reserves blocks, then uses mkswap and swapon Fast setup on ext4 and modern XFS Older filesystems may expose unwritten extents
dd Writes every byte before initialisation Broad compatibility with CentOS 7-era systems Slower and writes the entire file
Dedicated partition Activates a dedicated partition with swapon Predictable capacity for busy servers Requires partitioning and a reboot

Choose fallocate for the usual ext4 case. If mkswap rejects the file, initialise it with dd rather than masking the warning. A dedicated partition remains cleaner for sustained, high-volume paging.

Preparing local storage

Confirm the current layout and free capacity before creating anything:

sudo swapon --show
sudo lsblk -f
sudo df -hT /
findmnt -no FSTYPE /

Australian hosting shapes the storage choice. Sydney and Melbourne anchor the local AWS, Azure and Google Cloud markets, while Canberra public-sector workloads may require approved data-residency regions. For teams serving Perth, Adelaide or Brisbane, local storage avoids long-distance latency, and network filesystems commonly cannot support swap.

Put the file on a local filesystem with dependable free space. Keep it outside any directory that a cleanup job might remove, and choose a stable path such as /swapfile. Verify that the path is unused before allocation.

Creating and activating the file

Reserve the space, restrict access and create the swap header:

sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile

After fallocate, expect the allocated size to appear immediately in df; that is reservation, not a failed write.

On older filesystem combinations, mkswap can report an invalid or zeroed file because fallocate leaves unwritten extents. Recreate the contents safely rather than forcing past the warning:

sudo rm /swapfile
sudo dd if=/dev/zero of=/swapfile bs=1M count=2048 status=progress
sudo chmod 600 /swapfile
sudo mkswap /swapfile

Activate the prepared area and inspect it:

sudo swapon /swapfile
sudo swapon --show
sudo free -h

A successful swapon is normally silent. Use swapon --priority 10 /swapfile when multiple swap devices need a deliberate order.

Persisting swap across reboots

Add one entry to /etc/fstab so systemd activates the file at boot:

/swapfile none swap sw 0 0

Check for an existing entry before adding it, then reload the generated systemd units:

sudo systemctl daemon-reload

On SELinux-enforcing hosts, an audit denial may require a persistent swapfile_t context. If no equivalent rule exists and semanage is installed, apply one and restore the context:

sudo semanage fcontext -a -t swapfile_t /swapfile
sudo restorecon -v /swapfile

The Linux command reference provides related examples for reviewing fstab, SELinux contexts and systemd administration.

Verifying swap behaviour

Use several views because each answers a different question:

sudo swapon --show
sudo free -h
sudo cat /proc/swaps
vmstat 1 5

In vmstat, non-zero si and so values show pages being swapped in and out, usually in KiB/s. Sustained activity deserves investigation, while occasional movement during a memory burst can be normal. Correlate it with process memory and application limits.

Australian operations chats may call a warning “no worries” or “all good”, but keep activation evidence in command output and monitoring. Daylight saving changes AEST to AEDT, so timestamps should include the zone. For automated checks, the verification mindset in software testing practices complements testing swapon, reboot persistence and rollback.

Retiring or changing swap

Remove the /etc/fstab entry first and reload systemd; then disable and delete the file:

sudo systemctl daemon-reload
sudo swapoff /swapfile
sudo rm /swapfile

If swapoff fails, the kernel cannot move enough pages into RAM. Free memory or reduce the workload before retrying. To resize the area, disable it, recreate it at the new size, run mkswap, activate it with swapon and retain the original fstab path.

Never use swapoff -a blindly on a busy server, and do not remove a dedicated swap partition without checking whether hibernation or vendor requirements depend on it.

A reliable swap routine

A dependable procedure has four stages: inspect, create, validate and persist. Keep a small runbook with the chosen size, filesystem, permission mode, mkswap output, swapon --show result and exact fstab line. Test the sequence on a disposable host, then repeat it during a low-demand window.

Monitor swap-in and swap-out rates rather than treating any swap use as failure. Record memory limits beside the swap settings so a genuine application leak is not mistaken for a kernel problem. The practical takeaway is to keep swap local, permissioned, persistent and verifiable, so it absorbs brief pressure without turning memory exhaustion into a sudden outage.