How to Diagnose High CPU/Memory Usage on Arch Linux

How to Diagnose High CPU/Memory Usage on Arch Linux

When your Arch Linux system begins to slow down, lag, or feel unresponsive, it’s often due to unusually high CPU or memory usage. Understanding how to track down the root cause is crucial for system performance, stability, and even security. In this guide, we’ll explore step-by-step how to diagnose high CPU and memory usage on Arch Linux using both command-line tools and best practices.


🧠 Understanding the Basics

Before diving into tools, it’s important to understand the difference between CPU and memory usage:

  • CPU Usage: Refers to how much processing power is being used by applications or system processes. High CPU usage can slow down system responsiveness and cause thermal issues.
  • Memory Usage (RAM): Indicates how much volatile memory is being used. When RAM is full, the system may start swapping, which leads to degraded performance.

Arch Linux, being a minimal and DIY distribution, gives you complete control over system monitoring. However, this also means you’re expected to manually diagnose issues. Let’s get started.


🛠 Step 1: Check System Load

1. top and htop

One of the first tools most admins use is top or its more user-friendly cousin htop.

Install htop (if not installed)

sudo pacman -S htop

Run

htop

This provides a colorful, interactive display showing:

  • CPU cores usage.
  • Memory and swap usage.
  • Processes sorted by resource consumption.
  • Ability to sort by CPU, memory, time, etc.

You can also kill processes directly from htop by pressing F9.

What to look for

  • Are one or more processes consuming a large percentage of CPU?
  • Is RAM near full capacity?
  • Is the system using swap (indicated by Swp value)?

🔍 Step 2: Analyze CPU Usage in Detail

1. pidstat – Per-process CPU usage over time

Install via:

sudo pacman -S sysstat

Run:

pidstat 1

This will show per-process CPU usage every second. Helpful to see if CPU spikes are consistent or intermittent.

2. mpstat – Overall CPU usage

Also part of the sysstat package:

mpstat -P ALL 1

This shows CPU usage per core. If a single core is maxed while others are idle, you may be facing single-threaded bottlenecks.

3. perf top – Kernel-level profiling

For deeper insights into what’s happening in the kernel:

sudo pacman -S perf
sudo perf top

This tool provides real-time sampling of function calls, allowing you to understand what the CPU is spending time on, especially useful for debugging high usage in kernel space or low-level applications.


💾 Step 3: Investigate Memory Usage

1. free -h

Quick overview of memory usage:

free -h

Focus on the used, free, and available memory. High used memory isn’t always bad—Linux uses free RAM for buffers and cache—but available should not be critically low.

2. vmstat

Provides more detailed memory usage, including paging:

vmstat 1

Key columns:

  • si and so = Swap in/out (non-zero values suggest memory pressure).
  • buff, cache = Buffers and cached data.

3. smem

More detailed breakdown of memory per process:

sudo pacman -S smem
smem -r | sort -k 4 -nr | head

This lists processes using the most Proportional Set Size (PSS), a more accurate measure of memory usage.


📦 Step 4: Identify Memory Leaks or Misbehaving Services

Some background services or applications may leak memory or misbehave. Use these strategies to track them down:

1. ps_mem

A Python script that offers a better breakdown of memory usage:

yay -S ps_mem
sudo ps_mem

It aggregates child processes, making it easy to see how much memory entire services (like a web server) are consuming.

2. Systemd Service Analysis

If a systemd service is misbehaving:

systemctl status your-service

Or check for overall usage with:

systemd-cgtop

This tool shows CPU and memory usage of systemd control groups, which helps track services like nginx, docker, etc.


🌐 Step 5: Check for Resource-Heavy Applications

You might be running a web browser with too many tabs, a video editor, or a VM consuming significant resources.

1. xrestop (for X11 users)

To monitor X resource usage:

sudo pacman -S xrestop
xrestop

Great for catching runaway desktop applications hogging resources.

2. Browser Task Managers

Modern browsers like Firefox and Chromium have built-in task managers (Shift + Esc) to show tab/resource consumption.


🕵️ Step 6: Check Logs for Clues

Sometimes high usage is triggered by errors or misconfigurations. Check your logs:

journalctl -p 3 -xb

This will show only errors from the current boot. You can also monitor logs in real time with:

journalctl -f

Look for repetitive messages, crashes, or services stuck in loops.


🧰 Step 7: Use Profiling Tools for Long-Term Monitoring

1. glances

Install with:

sudo pacman -S glances

Run with:

glances

It gives an all-in-one dashboard of CPU, RAM, disk I/O, and network usage, and can be configured to run as a web service.

2. atop

Captures snapshots over time:

sudo pacman -S atop
atop

You can even schedule it as a systemd service to analyze problems retrospectively.


⚙️ Step 8: Tuning and Mitigation

Once you find the culprit, you have a few options:

1. Restart/Stop the Process

If a service is misbehaving:

sudo systemctl restart service-name

Or kill the process:

kill -9 <PID>

2. Limit CPU Usage with cpulimit

Example:

sudo pacman -S cpulimit
cpulimit -p <PID> -l 30

This restricts the process to 30% of CPU usage.

3. Limit Memory Usage with cgroups

You can create control groups to limit resources for users or services.

Example using systemd:

# /etc/systemd/system/your-service.service.d/override.conf
[Service]
MemoryMax=500M
CPUQuota=50%

Reload systemd and restart the service:

sudo systemctl daemon-reexec
sudo systemctl restart your-service

🚨 Bonus Tip: Check for Malware or Cryptominers

Unusual CPU spikes, especially when idle, might be caused by a malicious process (e.g., unauthorized crypto mining).

Scan with rkhunter or chkrootkit:

sudo pacman -S rkhunter
sudo rkhunter --check

Also verify running processes:

ps aux | grep -E "minerd|cryptonight|xmrig"

🧩 Final Thoughts

Diagnosing high CPU or memory usage on Arch Linux isn’t a one-size-fits-all process. It requires a combination of observation, analysis, and appropriate tooling. By mastering these command-line utilities and monitoring strategies, you’ll be well-equipped to identify and resolve performance issues efficiently.

Whether it’s a misbehaving desktop application, a runaway background process, or even a security threat, the steps outlined in this guide will help you narrow down the cause and take effective action.

Arch empowers users with full transparency and control. Use that power wisely, and your system will reward you with peak performance and stability.