Understanding VPS swap
When RAM fills up, the kernel can move rarely used pages to disk: that is swap. It avoids an immediate stop, but it is hundreds of times slower than memory. On a container VPS, swap is not configured like on a physical machine, and mistaking it for extra RAM leads to bad decisions.
What swap does
Swap absorbs temporary peaks and keeps genuinely active pages in memory. It adds no capacity: if RAM and swap are both full at the same time, the kernel triggers the OOM killer, which kills a process to free memory. For an application this means a brutal restart, not a gradual slowdown.
Check what is available
free -h
swapon --show
cat /proc/swaps
grep -E 'SwapTotal|SwapFree' /proc/meminfoIf SwapTotal is 0, no swap is visible inside the container. That is not necessarily a fault: in a container, memory and swap may be managed by the platform, and the value exposed to the container may stay at zero even when the platform has swap.
What a system container allows
A NeoHeberg VPS is an isolated system container. You are root inside it, but you do not have rights on the infrastructure that runs it. Practical consequences:
- creating a swap file with
fallocatethenswapon /swapfileusually fails withOperation not permitted; mknodis denied, so no manual swap device;/proc/sys/vm/swappinessis generally read-only in the container namespace;- adding a swap line to
/etc/fstabcan prevent a clean boot.
Do not try to tune swap as on a dedicated server. Treat it as a platform characteristic and work on actual consumption.
Reduce memory pressure
First measure who consumes memory:
free -h
ps -eo pid,rss,comm --sort=-rss | head -n 15
vmstat 1 5
sudo journalctl -k | grep -i oomThe si/so columns in vmstat show memory exchanged with disk. A sustained non-zero value means real pressure. Then tune the biggest consumers:
- PHP-FPM: lower
pm.max_children, addpm.max_requeststo recycle workers; - MariaDB: adjust
innodb_buffer_pool_size(often 25 to 50% of RAM); - Node.js: cap the heap with
--max-old-space-size; - Redis: set
maxmemoryand an eviction policy; - Nginx: reduce
worker_processesandworker_connectionsif needed.
Restart the relevant service, then measure again. One change at a time tells you what had an effect.
Good practice
- Do not disable swap without reason: it protects against peaks.
- Do not rely on it for a permanent load.
- Watch the trend over several days, not a single snapshot.
- If processes are still killed despite reasonable settings, move to a plan with more RAM: swap is not a capacity plan.
Advertisement
Was this article helpful?
Read next
- Monitor VPS uptimeSet up an external check, useful alerts and a method to tell network, server and application failures apart.
- Clean logs and cacheFree disk space without breaking a service by handling logs, packages, caches and Docker images in the right order.
- Automate security updatesConfigure unattended-upgrades to fix vulnerabilities unattended, then verify the mechanism actually runs.