Operations 9 min read

Using top and htop for Real‑Time System Resource Monitoring and Performance Analysis

This guide explains how to use the Linux utilities top and htop to monitor CPU, memory, disk I/O and network usage in real time, record performance data, analyze bottlenecks, and apply advanced techniques such as per‑process tracking, logging, chart generation and optimization recommendations.

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
Using top and htop for Real‑Time System Resource Monitoring and Performance Analysis

Introduction top and htop are powerful command‑line tools for real‑time monitoring of system resources, helping you identify performance bottlenecks and optimise applications.

1. Real‑time monitoring of system resources

Using top Start with the command top . Focus on CPU usage (%Cpu(s) line, especially us, sy, id), memory usage (KiB Mem and KiB Swap), and process list (press P to sort by CPU or M to sort by memory). Interactive commands include:

h or ? : show help
k : kill a process (enter PID)
r : renice a process (enter PID and new nice value)
q : quit top
1 : show each CPU core
f : field manager to select columns
o : sort‑option manager
W : write current settings to config file

Using htop Start with htop . It displays per‑CPU usage, memory and swap usage, and allows sorting via F6 . Interactive commands include:

F1 : help
F2 : settings menu
F3 : search processes
F4 : filter processes
F5 : tree view
F6 : choose sort field (e.g., %CPU)
F7 / F8 : increase or decrease nice value
F9 : kill process
F10 : quit htop

2. Monitoring specific processes To watch a known PID, use:

top -p 1234

or

htop -p 1234

3. Recording performance data Use top’s batch mode to capture snapshots:

top -b -n 1 > top_output.txt

Continuously record with watch :

watch -n 1 'top -b -n 1' > top_output.txt

For htop, script the output, e.g.:

script -c "htop" -t 2>&1 | tee htop_output.log

4. Analyzing data After collecting data, use a text editor or tools like gnuplot to plot trends.

5. Identifying bottlenecks Common issues are high CPU usage, memory exhaustion, excessive disk I/O, or network I/O.

Advanced topics

CPU usage analysis Sort by CPU with top -o %CPU or press 1 in top/htop to view per‑core stats. Examine the us (user) and sy (system) fields to pinpoint kernel vs. application load.

Memory usage analysis Sort by memory with top -o %MEM and inspect the KiB Mem line (free, used, buff/cache). Check KiB Swap for swap pressure.

Disk I/O analysis Use iostat -x 1 for extended statistics or iotop for per‑process I/O.

Network I/O analysis Tools like iftop and nethogs show bandwidth usage per host or per process.

Detailed process analysis In top, press c to display full command lines; in htop, press F5 for a tree view and F2 to enable custom thread names.

Saving and analysing logs Batch‑mode top can be scheduled with cron or watch (e.g., watch -n 5 'top -b -n 1' > top_output.txt ). htop output can be captured with the script command as shown above.

Combining other tools Use vmstat 1 for virtual memory stats, mpstat -P ALL 1 for per‑CPU stats, and sar -u 1 from the sysstat package for historical analysis.

Generating charts Plot data with gnuplot, e.g.:

gnuplot -e "set terminal png; set output 'cpu_usage.png'; plot 'top_output.txt' using 1:9 with lines title 'CPU Usage'"

Performance optimisation suggestions Improve code algorithms, increase CPU/memory resources, apply load balancing, introduce caching, optimise database queries and indexes, and fine‑tune network configuration to reduce latency.

operationsLinuxsystem monitoringtopPerformance analysishtop
Test Development Learning Exchange
Written by

Test Development Learning Exchange

Test Development Learning Exchange

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.