Swap provides storage that Linux can use for some memory pages when needed. It can help absorb memory pressure, but sustained swapping is slower than RAM and does not fix an application memory leak.
Check existing swap
Run:
swapon --show
free -hNo rows from swapon --show indicate no active swap areas; free -h still reports memory. Confirm that your hosting environment permits swap and that sufficient storage is available.
Create a swap file
These steps require administrator access and a filesystem that supports this type of swap file. Btrfs and some other configurations need different preparation; follow their supported procedure instead.
-
Confirm
/swapfiledoes not already exist. Do not overwrite an existing file. -
Create a fully allocated 1 GiB example file:
sudo dd if=/dev/zero of=/swapfile bs=1M count=1024 status=progress sudo chmod 600 /swapfile -
Initialise and activate that file:
sudo mkswap /swapfile sudo swapon /swapfile -
Check
swapon --show. If activation fails, investigate the filesystem or environment instead of forcing it. -
Back up
/etc/fstaband add one entry if it does not already exist:/swapfile none swap sw 0 0
Use a dedicated partition only when planned
Partitioning and formatting destroy existing data. Have an administrator identify an unused, specifically allocated partition and prepare it through the distribution’s documented procedure. Never run mkswap against a whole system disk or guess a device name. Use the resulting swap UUID for a persistent fstab entry.
Review swappiness only if needed
vm.swappiness influences the kernel’s preference for swapping; it is not a universal percentage threshold. Inspect it with:
cat /proc/sys/vm/swappinessFor a deliberate temporary example, use sudo sysctl vm.swappiness=10. A persistent setting uses the full key, such as vm.swappiness = 10, in an appropriate file under /etc/sysctl.d/. Choose a value based on the workload rather than assuming 10 is correct for every server.