Troubleshooting

High CPU or RAM usage on a Linux VPS: find the cause and fix it

Track down what is eating CPU or memory on your Linux VPS with top, ps and logs, then fix runaway PHP, MySQL, bots, Docker containers or a lack of swap.

IntermediateLinux VPS2 min read

A VPS that suddenly feels slow, returns 502/503 errors or kills processes is usually out of CPU or memory. The fix depends on what is using it, so start by measuring, not by upgrading. On shared cPanel hosting the same symptom appears as a resource limit; see 508 Resource Limit Reached instead.

Before you start

  • SSH access as root or a sudo user (connect over SSH).
  • If the server is unresponsive, reboot from your VPS control panel first, then investigate quickly before it fills up again.

See the overall picture

  1. Run uptime. A load average higher than your number of CPU cores means work is queuing (count cores with nproc).
  2. Run free -m. Look at available, not free; Linux uses spare RAM for cache.
  3. Run top (or htop) and press P to sort by CPU or M by memory.

Find the top processes

ps aux --sort=-%cpu | head -15
ps aux --sort=-%mem | head -15

Note the process name and user. For deeper digging see identify processes with lsof and monitor CPU, memory and disk.

Check for out-of-memory kills

dmesg -T | grep -i -E "killed process|out of memory"

If MySQL or PHP was killed, the server ran out of RAM. Adding swap gives breathing room while you fix the cause:

fallocate -l 2G /swapfile && chmod 600 /swapfile && mkswap /swapfile && swapon /swapfile

Add /swapfile none swap sw 0 0 to /etc/fstab to keep it after reboot.

Fix the usual suspects

ProcessTypical causeFix
php-fpm / lsphpToo many workers, bot traffic, heavy pluginCap workers with PHP-FPM pool tuning; add page caching
mysqld / mariadbdSlow queries, oversized buffersFind slow queries; size innodb_buffer_pool_size to the RAM you have
nginx / httpd busyBots, scrapers, a small DDoSCheck the access log for top IPs; rate limit or block
dockerd / containersOne container without limitsdocker stats, then set CPU and memory limits
Unknown binary, random name, 100% CPUPossible crypto-miner after a compromiseTreat as an incident: incident response on Linux

Find the busiest IPs in the web log

awk '{print $1}' /var/log/nginx/access.log | sort | uniq -c | sort -rn | head

Use /var/log/apache2/access.log or /var/log/httpd/access_log on Apache.

When to upgrade

If usage is high but legitimate (real visitors, normal queries) and caching is already on, the VPS is simply too small. Learn how cores map to load in VPS CPU cores explained.

Next steps

Was this guide helpful?

Still stuck?

Ask MdmHost AI for a quick answer, or open a ticket and our team will take a look. We are here 24/7.