High CPU or RAM

When the server is slow or not responding, often the cause is a process consuming too many resources. Here's how to diagnose and fix it.

02

Quick diagnosis

bash
# See CPU, RAM and processes in real time
htop

# If htop is not installed
top

In top / htop:

  • Sort by CPU: press P
  • Sort by RAM: press M
  • Kill a process: select it and press k, then enter 9
03

Find the "culprit" process

Top 5 processes by CPU

bash
ps aux --sort=-%cpu | head -6

Top 5 processes by RAM

bash
ps aux --sort=-%mem | head -6

Check load average

bash
uptime

If load average is much higher than number of CPU cores (nproc), the system is overloaded.

04

Kill a process

bash
# Get the PID of the process
ps aux | grep process-name

# Terminate gracefully (process can clean up and exit)
kill PID

# Forcefully terminate (if kill doesn't work)
kill -9 PID

# By process name (more convenient)
pkill process-name
killall process-name
05

Common causes of high CPU

1. Infinite loop in PHP/Python/other script

bash
# Find PHP processes
ps aux | grep php

If there are dozens of PHP processes, probably one script is looping. Kill PHP zombie processes:

bash
pkill -9 php-fpm
systemctl restart php8.2-fpm

2. Web scraper or DDoS attack

bash
# Check active connections
ss -tn | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -rn | head -20

If you see many connections from same IP, block it:

bash
ufw deny from IP_TO_BLOCK

3. MySQL / MariaDB with slow queries

bash
# Check MySQL processes
mysql -u root -p -e "SHOW FULL PROCESSLIST;"

# Kill a blocking query
mysql -u root -p -e "KILL QUERY PROCESS_ID;"

4. Service stuck in restart loop

bash
# Check failed services
systemctl --failed
journalctl -u service-name -n 50
06

Common causes of high RAM

Check what uses RAM

bash
free -h

# Details per process
ps aux --sort=-%mem | head -10

Free kernel cache (safe operation)

bash
sync && echo 3 > /proc/sys/vm/drop_caches

Kernel cache (buff/cache in free -h) is automatically freed when needed. It's not a problem if it's high: it's a Linux feature to use RAM efficiently.

OOM Killer: memory exhausted

If server ran out of RAM, kernel automatically kills processes (OOM Killer). Check if it happened:

bash
dmesg | grep -i "oom\|out of memory\|killed process"

If you see OOM messages, the plan's RAM might not be enough for the load. Consider:

  • Optimize services (reduce workers, configure swap)
  • Check for memory leaks in applications
  • Upgrade the plan
07

Add a Swap file (temporary solution)

If RAM is insufficient, you can add swap:

bash
# Create a 2GB swap file
fallocate -l 2G /swapfile
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile

# Make permanent
echo '/swapfile none swap sw 0 0' >> /etc/fstab

# Verify
free -h
swapon --show

Swap on VPS (SSD disk) is much slower than RAM. It's a temporary measure, not a permanent solution. If server frequently uses swap, it's time to upgrade.

DeluxHost, fondata nel 2023, offre soluzioni di hosting di alta qualità per diverse esigenze digitali. Forniamo hosting condiviso, VPS e server dedicati con sicurezza avanzata e datacenter globali.

© DeluxHost, Tutti i diritti riservati. | Partita IVA: IT17734661006
Tutti i sistemi operativi