Swap memory is disk space that Linux uses when physical RAM fills up. By creating a swapfile and registering it in fstab, you prevent processes from crashing under memory pressure. This article shows step by step how to create swap with fallocate and mkswap, how to set swappiness, and what to watch out for on a VPS with NVMe storage.
What is swap and when do you need it
Swap acts as overflow space when your server’s RAM is full. The operating system then moves inactive memory pages to disk, allowing active processes to keep running. Without swap, a server can forcibly terminate processes during memory shortages (the so-called OOM killer), leading to unexpected downtime.
Swap is especially useful on servers with little RAM, during peak loads, or as a buffer during updates and backups that temporarily require extra memory. On a VPS with NVMe storage, swap speed is less of a bottleneck than with traditional disks, but swap remains always slower than RAM. So use it as a safety net, not as a replacement for sufficient working memory.
Creating a swapfile on Linux
The simplest and safest way to add swap is a swapfile, rather than a separate partition. This works on virtually every Linux distribution and requires no repartitioning.
Check existing swap
Run swapon --show and free -h to see if swap is already active and how much free memory you have.
Create the swap file
Use fallocate -l 2G /swapfile to reserve a 2 GB file. If fallocate doesn’t work on your filesystem, use dd if=/dev/zero of=/swapfile bs=1M count=2048 instead.
Set the correct permissions
Restrict access with chmod 600 /swapfile, so that only root can read and write the file.
Format as swap
Run mkswap /swapfile to initialize the file as swap space.
Activate the swap
Turn on swap with swapon /swapfile and verify with swapon --show that it’s active.
Making swap permanent via fstab
Without extra action, swap disappears after a server restart. To prevent this, add a line to /etc/fstab.
Open the file with an editor, for example nano /etc/fstab, and add the following line at the bottom:
/swapfile none swap sw 0 0
Save the file and test the configuration with swapon -a. Then restart the server and check again with free -h whether the swap is automatically active. Before editing fstab, always make a copy of the original file so you can roll back if something goes wrong during boot.
If you’re running a managed server with Plesk, you can clearly monitor system settings and disk usage from the panel. If you’re unsure about the right configuration for your hosting environment, check the knowledge base for more background information.
Setting and managing swappiness
The kernel parameter swappiness determines how aggressively Linux uses swap relative to RAM. The value ranges from 0 to 100: a low value means the system only uses swap when really needed, a high value means it switches to disk more quickly.
Check the current setting with cat /proc/sys/vm/swappiness. Temporarily adjust the value with sysctl vm.swappiness=10. For a permanent change, add the line vm.swappiness=10 to /etc/sysctl.conf.
For web servers with databases, such as MySQL or PostgreSQL, a low swappiness is often advisable, as these applications are sensitive to delays caused by disk access. After making changes, monitor memory usage and server performance to determine whether the setting fits your workload.
If you’re considering switching to a hosting provider that configures servers well by default, check out the options to switch to Tandata without downtime.
Frequently Asked Questions
How much swap do I need?
This depends on the amount of RAM and the workload. As a rule of thumb: the less RAM, the larger the swap should be proportionally, but swap never replaces sufficient physical memory.
Is swap bad for an SSD or NVMe drive?
Modern NVMe storage handles swap activity just fine. With extremely heavy swap usage, wear can become a concern over time, but occasional use as a safety net is not an issue.
Can I remove swap?
Yes, first deactivate it with swapoff /swapfile, then delete the file and remove the line from fstab.
Does this also work on a VPS without root access?
No, creating and configuring swap requires root or sudo privileges on the system.
Does swap affect email servers?
Yes, memory shortages can also slow down or stall email processing. A stable hosting environment, such as with business email hosting, prevents this from being necessary.
Conclusion
Creating swap memory on Linux is a simple yet effective way to handle memory shortages. With fallocate, mkswap, and a line in fstab, you set up a stable swap configuration, and with the right swappiness setting, you tune the behavior to your workload. Want to leave this kind of management to a provider with fast NVMe servers and 24/7 support? Then check out the web hosting options.