What is swap space and when do you need it?
Swap space is an area on disk that Linux uses as an overflow for RAM. When physical memory fills up, the kernel moves inactive memory pages to swap, freeing RAM for active processes. Swap does not make your server faster — disk is far slower than RAM — but it acts as a safety net that prevents the kernel's Out-Of-Memory (OOM) killer from abruptly terminating processes when memory runs short.
Swap is especially valuable on small VPS and cloud servers with 1–4 GB of RAM, where a single traffic spike, a database query, or a composer/npm build can exhaust memory. It is also commonly required by control panels and application stacks that expect some swap to be present.
This tutorial targets Ubuntu 22.04 and 24.04 LTS, but the steps work on virtually any modern Linux distribution (Debian, Rocky, AlmaLinux) since the tools are identical. If you are provisioning a new server, you can apply these exact steps on a Skyline Cloud VPS.
Step 1 — Check for existing swap
Before creating swap, confirm that none already exists. Running two swap files is wasteful and can cause confusion.
sudo swapon --show
If the command returns nothing, you have no active swap. You can also see the memory and swap totals at a glance:
free -h
In the Swap row, a total of 0B confirms there is no swap yet.
Step 2 — Choose a swap size
There is no single correct size, but these guidelines are a sensible starting point for servers:
| RAM | Recommended swap |
|---|---|
| 1 GB | 2 GB |
| 2 GB | 2–4 GB |
| 4 GB | 4 GB |
| 8 GB or more | 2–4 GB (or none if you have plenty of RAM) |
Swap should not be a substitute for adequate RAM. If your server constantly leans on swap, the correct fix is to add memory — easily done by resizing a cloud plan rather than buying new hardware.
Step 3 — Create the swap file
Check that you have enough free disk space first:
df -h /
Create a 2 GB swap file. The fallocate command allocates the space instantly:
sudo fallocate -l 2G /swapfile
Verify the file size:
ls -lh /swapfile
If fallocate is unavailable or your filesystem does not support it cleanly (some setups need a fully written file), use dd instead. This is slower but always works:
sudo dd if=/dev/zero of=/swapfile bs=1M count=2048 status=progress
Step 4 — Secure and format the file
The swap file must be readable and writable only by root, or you expose its contents. Set permissions to 600:
sudo chmod 600 /swapfile
Now mark the file as a Linux swap area:
sudo mkswap /swapfile
You will see output confirming the swap label and a UUID.
Step 5 — Enable the swap
Activate the swap file immediately:
sudo swapon /swapfile
Confirm it is live:
sudo swapon --show
free -h
The Swap row in free -h should now show your 2 GB. Swap is active — but only until the next reboot.
Step 6 — Make swap permanent
To keep the swap file after a reboot, add it to /etc/fstab. Back up the file first:
sudo cp /etc/fstab /etc/fstab.bak
Append the swap entry safely with tee:
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
The fields mean: file path, mount point (none for swap), filesystem type (swap), options (sw), dump (0), and fsck pass (0). Verify the line was added correctly:
grep swapfile /etc/fstab
A typo here can stop the system from booting cleanly, so double-check the spelling and that you used >>/tee -a (append) rather than overwriting the file.
Step 7 — Tune swappiness
swappiness controls how aggressively the kernel moves data to swap, on a scale of 0–100. The default on Ubuntu is 60, which is fine for desktops but too eager for servers — it can push data to slow disk while RAM is still available.
Check the current value:
cat /proc/sys/vm/swappiness
For a typical server, 10 is a good value: use swap only when RAM is genuinely under pressure. Apply it now:
sudo sysctl vm.swappiness=10
Make it persistent across reboots:
echo 'vm.swappiness=10' | sudo tee /etc/sysctl.d/99-swappiness.conf
You can optionally lower vfs_cache_pressure to keep filesystem metadata cached longer:
echo 'vm.vfs_cache_pressure=50' | sudo tee -a /etc/sysctl.d/99-swappiness.conf
Step 8 — Verify after reboot (optional)
To be certain the configuration survives a restart, reboot and re-check:
sudo reboot
After logging back in:
sudo swapon --show
cat /proc/sys/vm/swappiness
Both should reflect your settings.
Removing or resizing swap
To remove swap, disable it, delete the line from /etc/fstab, then remove the file:
sudo swapoff /swapfile
sudo sed -i '/\/swapfile/d' /etc/fstab
sudo rm /swapfile
To resize, simply remove the old file and create a new one of the desired size using the steps above.
A note on SSDs and cloud disks
Modern NVMe and SSD storage handle swap writes well, and the small amount of wear from a sensibly tuned swap file is negligible. On a cloud server, swap also gives you breathing room while you decide whether to upgrade RAM. The cleanest long-term answer for a memory-bound workload is more RAM, not more swap.
Run it on a managed cloud server
Swap is a quick win, but a healthy server also needs reliable storage, snapshots, and headroom to scale RAM on demand. Skyline Cloud runs in-Kingdom with PDPL/NCA-aligned data residency, local Arabic support, and transparent pricing — so your data and your workloads stay in Saudi Arabia. Pair your VPS with business email hosting and managed backups to keep the whole stack covered.
Explore plans on the VPS and cloud servers hub, then create your Skyline Cloud account and have a tuned, production-ready server running in minutes.
Comments
0 total · 0 threads