When your VPS runs out of available RAM, the Linux kernel activates the OOM Killer, which abruptly terminates processes to free memory. The immediate solution is to identify what is consuming RAM and add swap space as a safety net.
Why Does a VPS Run Out of RAM?
VPS servers share physical hardware through virtualization, which means the RAM limit you purchased is a hard limit. Unlike a dedicated server, there is no extra memory to fall back on.
The most common causes of memory exhaustion include:
- Memory leaks in poorly written applications or faulty plugins.
- Traffic spikes that spawn too many PHP, Python, or Node processes simultaneously.
- MySQL or MariaDB configured with buffers too large for the available RAM.
- Unbounded caches (Redis, Memcached) growing without a configured ceiling.
How to Diagnose Your Current RAM Status
Before changing any configuration, you need to understand what is happening. Run these commands on your VPS:
# Quick memory summary
free -h
# Processes sorted by RAM consumption
ps aux --sort=-%mem | head -20
# Check if OOM Killer has already fired
dmesg | grep -i "oom\|killed" | tail -20
The free -h command shows total, used, free, and swap memory. If the available column is below 10% of total RAM, you are in the danger zone.
Reading the free -h Output
The available column is the most important: it shows how much memory the system can free for new processes without touching swap. A low value with high buffer/cache numbers means Linux can reclaim that cache if needed — not necessarily an emergency.
| Status | RAM available | Recommended action |
|---|---|---|
| Healthy | >25% of total | No urgent action needed |
| At risk | 10–25% of total | Review processes and configure swap |
| Critical | <10% of total | Act immediately |
How to Create and Enable Swap on Linux
Swap is a disk area the kernel uses when RAM is exhausted. It is not a substitute for RAM — it is much slower — but it prevents the OOM Killer from terminating critical processes while you address the underlying problem.
For VPS instances with SSD storage (most modern ones), a swap file of 1–2× available RAM is recommended. For more on VPS storage performance, browse our VPS server guides.
# 1. Create a 2 GB swap file
sudo fallocate -l 2G /swapfile
# 2. Set correct permissions
sudo chmod 600 /swapfile
# 3. Format as swap
sudo mkswap /swapfile
# 4. Enable it
sudo swapon /swapfile
# 5. Verify
free -h
To make swap persistent across reboots, add it to /etc/fstab:
/swapfile none swap sw 0 0
Tuning the Swappiness Parameter
The vm.swappiness parameter controls how aggressively the kernel moves data from RAM to swap. The default is 60; for a web server, lowering it to 10–20 is advisable.
# Temporary change
sudo sysctl vm.swappiness=10
# Permanent change (add to /etc/sysctl.conf)
vm.swappiness=10
Free Up RAM Without Rebooting
If your server is in crisis right now, you can recover memory without a reboot:
- Restart non-essential services:
sudo systemctl restart php-fpm. - Drop the kernel page cache (use with care):
sudo sync && echo 3 | sudo tee /proc/sys/vm/drop_caches. - Identify and kill processes consuming excessive memory:
sudo kill -9 <PID>.
Long-Term Solutions
Swap fixes the emergency, not the root cause. Permanent improvements include:
- Optimize MySQL/MariaDB: set
innodb_buffer_pool_sizeto at most 50–60% of your RAM. - Limit PHP-FPM workers: reduce
pm.max_childrenbased on memory available per process. - Cap your object cache: configure
maxmemoryin Redis. - Upgrade your VPS plan: if legitimate usage exceeds available RAM, it is time to scale. Explore options at elenlace.com.
Need help sizing your server correctly? The team at elenlace.com can help you choose the right plan without paying for resources you do not need.
Key Takeaways
- When a VPS runs out of RAM, the OOM Killer terminates processes — swap prevents this.
- Use
free -handps aux --sort=-%memto diagnose the problem. - Create a swap file of 1–2× RAM size and set
vm.swappinessto 10. - Optimize MySQL buffers, PHP-FPM workers, and cache limits to fix the root cause.
- If RAM usage is consistently high, it is a signal that you need more resources.
Still running out of RAM after these optimizations? Visit elenlace.com and explore higher-memory VPS plans built for growing sites and applications.
FAQ
Does swap replace RAM on a VPS?
No. Swap is a safety net, not a replacement. Disk reads and writes are 10 to 100 times slower than RAM, so a server that constantly relies on swap will experience severe performance degradation. Use it as a buffer while you optimize or upgrade your plan.
How much swap should I configure?
For VPS instances with less than 4 GB of RAM, set swap to 1–2× the RAM amount. For VPS with 4–8 GB of RAM, 2–4 GB of swap is sufficient. With more than 8 GB of RAM you rarely need swap, but keeping 2 GB configured is still a sensible safety measure.
Can the OOM Killer terminate MySQL or Nginx?
Yes, it can kill any process. You can adjust a process's oom_score_adj value to make the kernel avoid it as a last resort, but the correct solution is to ensure there is always enough RAM or swap available.
How do I know if the OOM Killer has already fired on my server?
Run dmesg | grep -i "oom\|killed" or check system logs in /var/log/messages or /var/log/syslog. You will see lines containing "Out of memory: Kill process" followed by the name and PID of the terminated process.
Compare providers
Other providers and guides worth comparing: