A Linux server with no swap has one response to running out of memory: the kernel picks a process and kills it. On a 2GB VPS that is usually your database, your web app, or a build you were waiting on. The process disappears with no error in its own log, and the server carries on as if nothing happened.
Every D4 Networks VPS starts with no swap, whatever the plan. This is normal for cloud images, because swap on a shared disk hides memory problems instead of solving them. It also means that the first time memory runs out, a process is killed.
1. Confirm the kernel killed it
Check memory first:
$ free -hIf the Swap line reads 0B, you have none. Then look for the kill:
$ sudo journalctl -k --since yesterday | grep -iE 'out of memory|oom' $ sudo journalctl --since yesterday | grep -i 'oom-kill'
The first command searches kernel messages, which name the process that was killed and how much memory it held. The second catches the systemd side, where a service that was killed this way ends with Failed with result 'oom-kill'. If either returns lines, memory ran out. The process that was killed is usually the largest one running at the time, and often has nothing to do with the cause.
2. Create the swap file
A swap file is a plain file the kernel uses as spare memory. Two gigabytes suits the 2GB and 4GB plans. Bigger plans rarely need more than that.
$ sudo dd if=/dev/zero of=/swapfile bs=1M count=2048 status=progress $ sudo chmod 600 /swapfile $ sudo mkswap /swapfile $ sudo swapon /swapfile
dd writes real zeros to every block. Other ways of creating a file, such as fallocate or truncate, can leave gaps that the kernel cannot use, and swapon refuses the file. The swapon manual describes dd as the most portable way to create one. chmod 600 stops other users reading whatever the kernel pages out, and mkswap writes the header that marks the file as swap.
Confirm it is active:
$ swapon --show $ free -h
3. Make it survive a reboot
swapon only lasts until the next boot. Add the file to /etc/fstab:
/swapfile none swap defaults 0 0
Then check that the line works before a reboot depends on it:
$ sudo swapon --all --verboseThat command reads /etc/fstab and activates every swap entry in it. If it prints an error, fix the line now. A bad fstab entry can stop the server booting.
4. Tell the kernel how eagerly to swap
vm.swappiness decides how readily the kernel moves idle memory to swap. The default is 60, which is tuned for desktops. On a server, a lower value keeps your app in memory and only uses swap when memory is nearly gone.
vm.swappiness = 10
$ sudo sysctl --system $ cat /proc/sys/vm/swappiness
At 0 the kernel waits until free memory is nearly gone before touching swap at all, which is too late to be useful. 10 is a sensible server default.
5. Check the update timers are running
On Debian and Ubuntu, automatic security updates depend on a scheduled job called apt-daily-upgrade.timer. Some VPS images ship with it turned off to protect small servers from running out of memory during updates. Now that you have swap, that protection is no longer needed, and if the timer is off then unattended-upgrades never runs.
$ systemctl is-enabled apt-daily.timer apt-daily-upgrade.timerIf either line says disabled, turn them back on:
$ sudo systemctl enable --now apt-daily.timer apt-daily-upgrade.timerAlmaLinux and Rocky are unaffected. dnf-automatic runs from its own timer, and our images leave it alone.
6. When swap is the wrong fix
Swap on a VPS lives on the same disk as everything else. It stops the kernel killing processes, and it does nothing to make the server faster. If free -h shows swap in steady use instead of an occasional spike, the server does not have enough memory for what it runs, and every access to swapped memory is a disk read.
Two signs that you have outgrown the plan:
$ vmstat 1 5Steady non-zero numbers in the si and so columns mean memory is being swapped in and out continuously. And a load average that stays above your vCPU count while CPU use is low usually means processes are waiting on that disk traffic.
At that point, upgrade the plan. Our guide on how much RAM and CPU your VPS needs covers what each plan comfortably runs.
Quick reference
| Task | Command |
|---|---|
| Check memory and swap | free -h |
| Find an OOM kill | sudo journalctl -k | grep -iE 'out of memory|oom' |
| Create the file | sudo dd if=/dev/zero of=/swapfile bs=1M count=2048 |
| Activate now | sudo mkswap /swapfile && sudo swapon /swapfile |
| Activate at boot | /swapfile none swap defaults 0 0 in /etc/fstab |
| Test the fstab line | sudo swapon --all --verbose |
| Set swappiness | vm.swappiness = 10 in /etc/sysctl.d/99-swap.conf |
| See swap traffic | vmstat 1 5, columns si and so |
| Check update timers | systemctl is-enabled apt-daily.timer apt-daily-upgrade.timer |
Where to go next
With swap in place, running low on memory shows up as slowness before it shows up as a dead process. To see it coming, read the memory graph in the client area; reading your VPS usage graphs explains what a healthy one looks like. And if a process keeps dying even with swap in place, vmstat and the memory graph together will show whether the server is short of memory all the time or only under bursts, which decides whether the answer is tuning or a bigger plan.