SWAP in Linux
Guide to creating and configuring swap space on Linux.
SWAP (swap space) is a virtual memory mechanism in Linux that moves inactive data from RAM to disk (HDD or SSD) to free up physical memory for active tasks. It acts as an emergency buffer when RAM runs low, but disk access is thousands of times slower than RAM, so heavy swap usage noticeably slows down the system.
In Linux, RAM is divided into pages. Swapping is the process of copying less-used pages to a dedicated disk area called swap space (either a file or a dedicated partition). Together, RAM and swap form the total virtual memory available to the system.
Why swap is needed
Swap serves two main purposes:
- When the system needs more memory than is currently available, the kernel moves the least-used pages to swap, freeing RAM for active processes.
- Many pages loaded by applications at startup are rarely used afterward. The system can move them to swap, keeping RAM available for more important tasks.
Drawbacks of swap
ОThe main downside is speed. RAM operations happen in nanoseconds, while disk operations take milliseconds — tens of thousands of times slower. Heavy swap usage leads to noticeable slowdowns.
Note
When applications start heavily relying on swap, performance drops significantly. In such cases, consider increasing RAM. One option is to move your project to a more powerful VPS or dedicated server with flexible resource scaling.
Common signs of memory shortage (especially affecting databases):
- Frequent website errors like "database connection failed" (e.g., MySQL crashes)
- Logs showing InnoDB: Fatal error: cannot allocate memory for the buffer pool
- Slow server response even with low CPU usage
Even on SSDs, swap operations go through the disk subsystem, increasing latency.
Если swap активно используется — лучше увеличить объём RAM или оптимизировать проект. Один из вариантов — перейти на более мощный VPS или выделенный сервер.
Checking for swap
Run:
swapon -s
Example output:
Filename Type Size Used Priority
/swapfile file 40956 40956 -1
Or for a quick overview:
free -m
Example output:
total used free shared buff/cache available
Mem: 488 160 7 40 320 259
Swap: 39 39 0
If Swap shows 0 or is missing — you need to create it.
Creating a swap file (recommended method)
- Create a swap file (example: 2 GB):
sudo fallocate -l 2G /swapfile
or
sudo dd if=/dev/zero of=/swapfile bs=1M count=2048
- Set secure permissions (only root can read/write):
sudo chmod 600 /swapfile
- Format it as swap:
sudo mkswap /swapfile
- Enable the swap file:
sudo swapon /swapfile
- Make it permanent — add to
/etc/fstab:
sudo nano /etc/fstab
Add this line at the end:
/swapfile none swap sw 0 0
Save (Ctrl+O → Enter → Ctrl+X).
- Verify:
free -h
Optional: tuning swappiness
swappiness controls how aggressively the system uses swap (0–100, default 60).
Check current value:
cat /proc/sys/vm/swappiness
Set a lower value (recommended for servers: 10–20):
sudo sysctl vm.swappiness=10
To make it permanent, add to /etc/sysctl.conf:
vm.swappiness=10
Apply:
sudo sysctl -p
Help
If you have any questions or need assistance, please contact us through the ticket system — we're always here to help!