How to Diagnose and Fix High CPU Usage

By Raman Kumar

Updated on Jan 06, 2025

In this tutorial, we'll learn how to diagnose and fix high CPU usage.

High CPU usage on a Virtual Private Server (VPS) can degrade the performance of your applications and services. This guide provides a detailed approach to identifying and resolving the issue effectively.

Understanding High CPU Usage

High CPU usage occurs when the processes running on your VPS demand excessive computational resources. It can be caused by poorly optimized applications, excessive background tasks, or even malicious activities like Distributed Denial of Service (DDoS) attacks.

How to Diagnose and Fix High CPU Usage

Step 1: Identifying the Cause

1. Check Running Processes

List all running processes with:

ps aux --sort=-%cpu | head

The ps command provides a snapshot of active processes sorted by CPU usage.

2. Examine Cron Jobs

Check for scheduled tasks that might be causing spikes:

crontab -l

Also, examine system-wide cron jobs located in /etc/crontab or /etc/cron.d/.

3. Inspect Application Logs

Review application-specific logs for anomalies:

For Nginx:

sudo cat /var/log/nginx/error.log

For Apache:

sudo cat /var/log/apache2/error.log

4. Check Disk I/O

Disk I/O bottlenecks can increase CPU load. Use:

iostat -x 1

Look for high %iowait values indicating I/O-related issues.

5. Detect Malicious Activity

Run security checks to ensure no malicious software is causing the load:

Scan for rootkits:

sudo rkhunter --check

Check for unusual network activity:

sudo netstat -plntu

Step 2: Fixing High CPU Usage

1. Optimize Applications

Web Servers:

For Nginx:

Reduce worker processes in /etc/nginx/nginx.conf:

worker_processes auto;

Reload configuration:

sudo systemctl reload nginx

For Apache:

Optimize MaxRequestWorkers in /etc/apache2/apache2.conf:

MaxRequestWorkers 100

Restart Apache:

sudo systemctl restart apache2

Databases:

Analyze slow queries in MySQL:

SHOW FULL PROCESSLIST;

Tune configurations in my.cnf using tools like MySQLTuner.

2. Stop Unnecessary Services

Disable or stop services not required:

sudo systemctl stop <service_name>
sudo systemctl disable <service_name>

3. Limit Resource Usage

Implement process limits:

Install and configure cpulimit:

sudo apt install cpulimit
cpulimit -l 50 -p <pid>

4. Update Software

Outdated software might contain bugs causing inefficiencies. Update system packages:

sudo apt update && sudo apt upgrade -y

5. Increase Server Resources

If CPU usage consistently exceeds limits, consider upgrading your VPS plan to allocate more resources.

Step 3: Proactive Monitoring and Prevention

1. Install Monitoring Tools

Use Netdata for real-time system monitoring:

bash <(curl -Ss https://my-netdata.io/kickstart.sh)

Set up Prometheus and Grafana for advanced monitoring and visualization.

2. Configure Alerts

Set up alerts for abnormal CPU usage using monitoring tools or custom scripts.

3. Implement Security Measures

Enable a firewall:

sudo ufw enable

Install fail2ban to block suspicious IPs:

sudo apt install fail2ban

4. Schedule Regular Maintenance

Perform regular updates, clear old logs, and check resource usage periodically.

Diagnosing High CPU Usage with Tools Like htop and vmstat

Effectively diagnosing high CPU usage is crucial for understanding the root cause and implementing a resolution. Tools like htop and vmstat provide valuable insights into CPU utilization, processes, and system performance. Here’s how to use these tools in detail.

Using htop for Diagnosis

What is htop?

htop is an interactive process viewer that provides real-time information about system resource usage. It is more intuitive than top and allows you to scroll through processes, search, and sort based on different criteria.

Installing htop

If not already installed, you can install it with:

# On Debian/Ubuntu
sudo apt install htop

# On CentOS/AlmaLinux/RHEL
sudo yum install htop

# On Fedora
sudo dnf install htop

Launching htop

Run the command:

htop

Key Features of htop

  • CPU Bars: Colored bars at the top represent each CPU core’s usage, segmented into user processes, system processes, and idle time.
  • Process List: Shows all running processes, sorted by CPU usage by default.
  • Tree View: Press F5 to see processes in a hierarchical tree format, which helps identify parent-child relationships.
  • Search: Press / and type a process name to locate it.
  • Filter by CPU Usage: Sort processes by pressing F6 and selecting %CPU.

Identifying High CPU Usage

  • Look for processes at the top of the list consuming a high percentage of CPU (%CPU column).
  • Note the COMMAND column to identify the application or service responsible.

Resolving High CPU Usage Using htop

Kill a Process:

  • Select the process with arrow keys.
  • Press F9 and choose a signal (e.g., SIGKILL) to terminate it.

Renice a Process:   

  • Select the process.
  • Press F7 or F8 to increase or decrease its priority.

Common Issues and Fixes:

  • Runaway Processes: If a process is stuck in a loop, restart or optimize the application.
  • Background Tasks: Disable unnecessary background tasks that consume resources using:
sudo systemctl stop <service_name>
sudo systemctl disable <service_name>

Using vmstat for Diagnosis

What is vmstat?

vmstat (virtual memory statistics) is a command-line tool for monitoring system performance, including CPU, memory, and I/O activity. It provides an overview of the system's state, allowing you to detect CPU bottlenecks.

Installing vmstat

On most distributions, vmstat is included in the procps package. If not installed, use:

# On Debian/Ubuntu
sudo apt install procps

# On CentOS/AlmaLinux/RHEL
sudo yum install procps-ng

Basic Usage

Run vmstat with a sampling interval to see live data:

vmstat 1

This command updates statistics every second.

Key Metrics in vmstat:

Procs:

  • r: Number of processes waiting for CPU time. A high number indicates CPU overloading.
  • b: Number of processes in uninterruptible sleep, often related to I/O issues.

CPU:

  • us: Percentage of time spent on user processes.
  • sy: Percentage of time spent on system (kernel) processes.
  • id: Percentage of idle time. Low id indicates high CPU usage.
  • wa: Percentage of time spent waiting for I/O. High wa suggests disk or network I/O bottlenecks.

Interpreting Results

  • High us: Indicates CPU-intensive applications. Identify and optimize these processes.
  • High sy: Suggests kernel-level issues, possibly due to I/O or driver problems.
  • High wa: Points to I/O bottlenecks, such as slow disk performance or network issues.
  • Low id: Confirms that the CPU is under heavy load.

Resolving High CPU Usage Based on vmstat Output

High us Usage. Identify CPU-intensive applications with:

ps aux --sort=-%cpu | head

Optimize the application or service consuming the CPU. 

High wa usage: 

Check disk performance:

iostat -x 1

Ensure sufficient disk IOPS for your application. Upgrade to faster disks or optimize disk usage.

High sy Usage:

  • Look for system-level issues like excessive context switching or I/O operations.
  • Disable unnecessary services or offload tasks to other servers.

Combining htop and vmstat for Diagnosis

Using htop and vmstat together provides a comprehensive view of system performance:

  • Use vmstat to observe overall system trends, such as high us or wa times.
  • Switch to htop to pinpoint specific processes responsible for the CPU load.

Example: Resolving High CPU Usage

Scenario 1: A PHP-FPM Process Consuming High CPU

Diagnose:

  • Use htop to locate the PHP-FPM process with high %CPU.
  • Note the process ID (PID).

Action:

Restart the PHP-FPM service:

sudo systemctl restart php7.4-fpm

If the issue persists, optimize PHP configurations in php.ini.

Scenario 2: High wa Time Indicating I/O Bottleneck

Diagnose:

  • Run vmstat and note a high wa percentage.
  • Use iotop to find processes causing high I/O:
sudo iotop

Action:

  • Optimize the disk-intensive application or upgrade to SSD storage.

Scenario 3: Unnecessary Services Consuming Resources

Diagnose:

  • Use htop to identify unnecessary services.

Action:

Stop and disable the service:

sudo systemctl stop <service_name>
sudo systemctl disable <service_name>

Proactive Measures to Avoid High CPU Usage

1. Implement Monitoring Tools:

  • Install tools like Netdata or Prometheus for real-time alerts and dashboards.

2. Regular Maintenance:

  • Periodically review running processes and scheduled tasks.

3. Optimize Applications:

  • Profile and debug your code to eliminate inefficiencies.

4. Upgrade Resources:

  • If demand consistently exceeds capacity, consider upgrading your VPS.

By combining tools like htop and vmstat with proactive measures, you can effectively diagnose and resolve high CPU usage issues on your VPS, ensuring optimal performance.

Conclusion

High CPU usage on a VPS can affect your system’s performance and stability. Diagnosing the root cause with tools like htop, vmstat, and log analysis is essential. By optimizing applications, stopping unnecessary services, and monitoring the system proactively, you can ensure your VPS operates efficiently and reliably.

Checkout our instant dedicated servers and Instant KVM VPS plans.