Short answer: Swap space is disk space where the operating system temporarily moves rarely used memory pages when the server’s physical memory (RAM) fills up. It doesn’t make RAM bigger, but it’s a safety net that stops services from being killed when memory suddenly runs out. On Linux it’s configured as a separate partition (swap partition) or a file (swap file).
How Does Swap Space Work?
All applications, services and the operating system on a server share the same RAM. When total memory demand approaches physical RAM, the Linux kernel writes memory pages that haven’t been used for a while to disk, into swap space (swap out). When those pages are needed again, they’re read back from disk into RAM (swap in). This lets the system behave as if it had more memory than physical RAM; the concept is called virtual memory.
Why Swap Matters on a Server
- A buffer against the OOM Killer: when RAM is completely full and there’s no swap, the kernel’s OOM (Out of Memory) Killer terminates memory-using processes to keep the system alive. Often that’s the biggest consumer, such as the database or web server.
- Efficient use of RAM: memory pages loaded at startup and never used again are moved to disk, leaving RAM for active applications and the file cache.
- Sudden load spikes: it prevents a crash when memory is briefly exceeded during short traffic peaks or jobs such as backups.
Disk is much slower than RAM. If the system is constantly writing to and reading from swap (thrashing), everything slows down noticeably. That means RAM, not swap, is insufficient; the fix is adding RAM or reducing memory use, not enlarging swap.
Swap Partition vs Swap File
| Feature | Swap partition | Swap file |
|---|---|---|
| When it’s created | Usually during OS installation, as a separate disk partition | Any time while the system is running |
| Resizing | Hard; requires changing disk partitions (or LVM) | Easy; delete the file and create a new one at the new size |
| Performance | No file system layer; historically slightly faster | On modern kernels and SSDs the difference is negligible in practice |
| Best for | Initial setup of physical servers | VDS/VPS and servers where swap is added later |
Many VDS/VPS images come without swap. In that case the most practical approach is to add a swap file as shown below. If you’re wondering what a VDS is, see our what is a VDS article.
Creating a Swap File on a Linux Server
The steps below work on Ubuntu, Debian, AlmaLinux and Rocky Linux (on ext4 and XFS file systems). Systems using Btrfs need different steps for a swap file.
1. Check the current swap status. If swapon --show prints nothing, or the Swap line in free -h shows 0, there’s no swap:
sudo swapon --show
free -h2. Create the swap file (example: 4 GB) and make it readable and writable only by root. fallocate is fast; on file systems that don’t support it, use dd:
sudo fallocate -l 4G /swapfile
# if fallocate doesn't work:
# sudo dd if=/dev/zero of=/swapfile bs=1M count=4096
sudo chmod 600 /swapfile3. Format the file as swap and enable it:
sudo mkswap /swapfile
sudo swapon /swapfile
swapon --show4. Make it persistent across reboots. Add this line to the end of /etc/fstab:
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstabA typo in fstab can stop the server from booting; it’s a good habit to check the file with sudo findmnt --verify after any change.
What Is Swappiness and How Do You Set It?
vm.swappiness controls how willing the kernel is to move application memory to swap rather than drop file cache when memory gets tight. The default on most distributions is 60. Since Linux 5.8 the range is 0–200 (0–100 on older kernels).
- Low value (e.g. 10): swap is used as late as possible. Often chosen for latency-sensitive servers such as database servers.
- Default (60): a balanced setting for general-purpose systems.
- 0: doesn’t disable swap, only postpones it until the last moment; if memory fills up, the OOM Killer may step in sooner. Use with care.
# View the current value
cat /proc/sys/vm/swappiness
# Change it temporarily
sudo sysctl vm.swappiness=10
# Make it persistent
echo 'vm.swappiness=10' | sudo tee /etc/sysctl.d/99-swappiness.conf
sudo sysctl --systemHow Big Should Swap Be?
There’s no strict rule. The values Red Hat recommends in its RHEL documentation for systems that don’t use hibernation are a good starting point:
| Physical RAM | Recommended swap |
|---|---|
| 2 GB or less | 2× RAM |
| 2 GB – 8 GB | Equal to RAM |
| 8 GB – 64 GB | At least 4 GB |
| More than 64 GB | At least 4 GB |
On servers with plenty of RAM, swap is a buffer; rather than allocating a huge swap, monitor memory use (free -h, vmstat 1) and add RAM if swap usage stays high.
Frequently Asked Questions
Is high swap usage a problem?
Not on its own. It’s normal for pages that haven’t been used for a long time to sit in swap. The real warning sign is the si/so (swap in/out) columns in vmstat 1 staying above zero, which means the system is actively writing to and reading from disk.
How do I empty swap temporarily?
sudo swapoff -a && sudo swapon -a moves the pages in swap back into RAM. Only do this when free RAM is larger than used swap; otherwise the OOM Killer may step in.
Does using swap on an SSD wear it out?
Occasional swap writes aren’t a problem for the endurance of today’s SSDs. Constant heavy swap use hurts both performance and the disk; in that case you should add RAM.
Conclusion
Swap space is a safety net that stops services from being killed when RAM fills up; it exists for stability, not performance. On VDS/VPS servers you can add a swap file with a few commands and tune its use for your workload with swappiness. If swap is constantly in use, moving to a VDS server plan with more RAM is the lasting fix.
Categories
-
CentOS 7 guides
-
cPanel 20 guides
-
CyberPanel 18 guides
-
Operating Systems 81 guides
-
Juniper Networks 18 guides
-
Control Panels 50 guides
-
Linux 17 guides
-
MySQL 29 guides
-
Network 95 guides
-
Plesk Panel 12 guides
-
Virtual Servers 273 guides
-
SEO 3 guides
-
Cyber Security 8 guides
-
Technology 162 guides
-
VMware 36 guides
-
WordPress 20 guides
-
Artificial Intelligence 22 guides