VPS Running Out of Memory: Diagnosing OOM and Adding Swap

The site dies at midnight, the database restarts for no reason, SSH lags until you cannot connect — the most common cause behind these symptoms is memory exhaustion. This article shows how to confirm it, apply first aid, and fix it for good.

Step 1: confirm it is memory

Log in via SSH and run free -h, focusing on the available column: consistently under 200 MB means memory pressure. Then run dmesg | grep -i kill — if you see Out of memory: Killed process, the kernel's OOM Killer has been terminating processes, which is exactly why your services "mysteriously disappear".

Step 2: find what is eating memory

Run top and press Shift+M to sort by memory. Usual suspects:

  • MySQL: default settings assume plenty of RAM — on small machines lower innodb_buffer_pool_size;
  • PHP-FPM: too many worker processes — estimate max_children as available RAM ÷ per-process usage;
  • Runaway apps: a leaking Node.js or Java process grows until everything is consumed.

Step 3: add swap as first aid

Swap is disk space acting as backup memory, preventing the OOM Killer from striking immediately. VPS images usually ship without swap. To add 2 GB: create a swap file with fallocate, set permissions with chmod 600, format with mkswap, enable with swapon, and add it to /etc/fstab so it survives reboots. Remember swap is only a buffer: when RAM truly runs short the system slows down instead of crashing — buying you time to investigate.

The long-term fix

If available memory still hugs the floor after tuning and swap usage stays high, your workload has outgrown the plan — upgrading RAM is the most cost-effective fix. 00Shark supports in-place plan upgrades; contact support with your current usage for an assessment.

Cet article a été rédigé avec l'aide de l'IA par l'équipe éditoriale de SharkCloud et relu avant publication.