Kahibaro
Discord Login Register

CPU and memory monitoring

Why CPU and Memory Monitoring Matters

On a running Linux system, CPU and memory are usually the first bottlenecks you’ll notice when things “feel slow.” Monitoring them helps you:

This chapter focuses on practical CPU and memory monitoring using standard tools on most Linux systems.

Key Concepts for CPU Monitoring

You don’t need deep kernel internals here, but a few concepts help interpret tool output correctly.

High CPU is not always bad; high CPU doing useful work can be expected. Problems usually arise when:

Key Concepts for Memory Monitoring

Linux memory usage can look “full” even on healthy systems because the kernel aggressively uses memory for cache. Important distinctions:

Warning signs:

Using `top` for Interactive Monitoring

top is installed on almost all Linux systems and shows a live view of processes and resource usage.

Run:

top

CPU Section in `top`

Near the top, you’ll see a line like:

%Cpu(s): 10.0 us,  2.0 sy,  0.0 ni, 80.0 id,  5.0 wa,  0.0 hi,  3.0 si,  0.0 st

Common fields:

Reading it:

Per-CPU View in `top`

By default, top shows overall averages. To split by CPU:

This is useful when:

Memory Section in `top`

Typical lines:

MiB Mem :  7859.0 total,   500.0 free,  2000.0 used,  5359.0 buff/cache
MiB Swap:  2047.0 total,  2047.0 free,     0.0 used.  4000.0 avail Mem

Important numbers:

If avail Mem is low and swap usage is increasing, the system is under memory pressure.

Focusing on CPU or Memory-Hungry Processes in `top`

In the process list:

Useful shortcuts:

This lets you quickly identify which processes consume the most CPU or RAM.

Using `htop` for a More User-Friendly View

htop is an enhanced alternative to top (not always installed by default, but commonly available via your package manager).

Run:

htop

Key advantages over top:

Typical bars:

Sorting and Filtering in `htop`

htop is ideal for quickly spotting spikes, runaway processes, and verifying that all cores are being utilized.

Using `vmstat` for System-Wide Trends

vmstat provides a snapshot of virtual memory, processes, and CPU activity.

Run once:

vmstat

Or run repeatedly every second:

vmstat 1

Typical output:

procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 1  0      0 120000  20000 500000    0    0     5     2  100  200 10  5 80  5  0

Useful columns:

vmstat is particularly useful to watch trends over time at a low overhead, for example when reproducing an issue.

Using `mpstat` for Per-CPU Monitoring

mpstat (from the sysstat package on many distros) focuses on CPU usage, especially per-CPU breakdowns.

To see overall usage every 2 seconds:

mpstat 2

To see per-CPU usage:

mpstat -P ALL 2

Typical fields:

Use cases:

Using `free` to Inspect Memory Usage

free shows a quick summary of RAM and swap.

Run:

free -h

Example:

              total        used        free      shared  buff/cache   available
Mem:           7.7G        2.0G        0.5G        0.2G        5.2G        4.0G
Swap:          2.0G          0B        2.0G

Interpretation:

Using `/proc/meminfo` for Detailed Memory Stats

For more detailed memory information:

cat /proc/meminfo

This file contains many fields; common ones:

Use /proc/meminfo when you need more precision than free gives, or when writing scripts that parse memory values.

Using `ps` to Identify Heavy Processes

While top and htop are interactive, ps is good for one-off snapshots or scripts.

Examples:

  ps aux --sort=-%cpu | head -n 11
  ps aux --sort=-%mem | head -n 11

Common columns:

ps excels when you need to log or script resource checks, not just visually inspect them.

Using `sar` for Historical CPU and Memory Data

Real-time tools only show the present moment. For historical analysis, sar (also in sysstat) can log CPU and memory usage over time.

Depending on your distribution, you may need to:

  1. Install sysstat.
  2. Enable and start its data collection service or cron job.

Examples (after data collection is enabled):

  sar -u
  sar -P ALL
  sar -r

sar is useful to answer questions like “What was CPU and memory usage at 3 PM yesterday?” or to see patterns over days.

Basic Patterns and How to Interpret Them

CPU Bottlenecks

Signs:

Possible actions (overview):

Memory Bottlenecks

Signs:

Possible actions (overview):

Simple Scripting Ideas for CPU/Memory Checks

Even at an intermediate level, you can automate basic checks.

Example: warn if CPU idle falls below 10%:

#!/bin/bash
idle=$(vmstat 1 2 | tail -1 | awk '{print $15}')
if [ "$idle" -lt 10 ]; then
  echo "Warning: low CPU idle: ${idle}%"
fi

Example: warn if available memory below 500 MB:

#!/bin/bash
avail=$(awk '/MemAvailable/ {print $2}' /proc/meminfo)  # in kB
if [ "$avail" -lt 512000 ]; then
  echo "Warning: low available memory: $((avail / 1024)) MB"
fi

These are simple examples; later chapters will cover more robust monitoring and alerting using dedicated tools.

Choosing the Right Tool

As a quick guide:

Experiment with these tools on a lightly loaded system and then under load (e.g., while compiling software or running a CPU/memory-intensive program) to build intuition about what “normal” and “problematic” states look like.

Views: 21

Comments

Please login to add a comment.

Don't have an account? Register now!