Essential for Terminal: Monitoring System Resources with Ubuntu's top Command

In the Ubuntu system, the terminal is a crucial tool for managing and monitoring the system. When you need to stay informed about real-time system resource usage—such as whether the CPU is heavily utilized, if memory is tight, or which processes are “secretly” consuming resources—a reliable system monitoring tool becomes essential. The top command is one of the most fundamental and practical system monitoring utilities in the Ubuntu terminal. It dynamically displays the current system status, including CPU, memory, and process information, allowing you to fully grasp the system’s “pulse.”

1. Starting the top Command

To use the top command, simply open a terminal (quickly accessible via the shortcut Ctrl+Alt+T), then type top and press Enter. For regular users, executing top directly will display basic system resource monitoring information. To view more detailed system-level data (e.g., process UIDs), run it with sudo for administrative privileges. Beginners can start with regular permissions first.

2. Detailed Explanation of the top Interface

After starting top, you’ll see a dynamically updating interface divided into several sections:

First Line: System Overview

  • 12:34:56: Current system time
  • up 1 day, 2:30: System uptime (e.g., 1 day and 2 hours 30 minutes)
  • 1 user: Number of current logged-in users
  • load average: 0.12, 0.15, 0.18: System load average, where the three values represent the average load over 1, 5, and 15 minutes. Generally, a load value not exceeding the number of CPU cores is normal (e.g., for a 4-core CPU, load < 4). A higher value may indicate an overload of system tasks.

Second Line: Process Summary

  • Tasks: 120 total: Total number of processes
  • 1 running: Number of running processes (status R)
  • 118 sleeping: Number of sleeping processes (status S)
  • 0 stopped: Number of stopped processes
  • 2 zombie: Number of zombie processes (non-zero values require checking for abnormal process exits)

Third Line: CPU Status

  • %Cpu(s): 2.0 us, 1.0 sy, 0.0 ni, 96.0 id, 1.0 wa, 0.0 hi, 0.0 si, 0.0 st: Detailed breakdown of CPU usage:
  • us: CPU time used by user-space processes (e.g., programs you run)
  • sy: CPU time used by kernel-space processes (system management programs)
  • id: Idle CPU time (ideally as high as possible, ideally near 100% for system idle time)
  • wa: CPU time waiting for I/O (e.g., disk/network read/write). A high wa (>20%) may indicate an I/O bottleneck (e.g., slow hard drive).

Fourth Line: Memory Status

  • KiB Mem : 16384000 total, 1234567 free, 8765432 used, 5678901 buff/cache:
  • total: Total physical memory (e.g., ~16GB in the example)
  • used: Memory in use (including cache and buffers)
  • free: Free memory
  • buff/cache: Buffer and cache (system-cached data, treat as “available reserve” rather than actual used memory).

Fifth Line: Swap Space

  • KiB Swap: 2097152 total, 2097152 free, 0 used: Swap space (similar to Windows virtual memory). If used is non-zero, physical memory may be insufficient, and the system is using swap space.

Below: Process List (Core Section)

This displays detailed information for all processes (default columns):
- PID: Process ID (unique identifier)
- USER: Process owner
- %CPU: Real-time CPU usage
- %MEM: Real-time memory usage
- S: Process status (R running, S sleeping, D uninterruptible sleep, Z zombie)
- COMMAND: Process name (e.g., chrome, python).

3. Common top Shortcuts (Essential for Beginners)

While top is running, use these shortcuts to avoid restarting:
- q: Quit top (most common)
- P: Sort processes by CPU usage (descending, quickly find “CPU-hungry” processes)
- M: Sort processes by memory usage (descending, quickly find “memory-hungry” processes)
- 1: Show per-CPU core usage (critical for multi-core CPUs, e.g., 4 cores will display 4 CPU rows)
- k: Terminate a process (enter the target PID, press Enter to confirm)
- h: Show help (lists all shortcuts; press q to return)
- t: Toggle display of CPU/memory summary info
- m: Toggle detailed memory usage display

4. Practical Scenarios

Scenario 1: Troubleshooting High CPU Usage

  1. After starting top, press P to sort by CPU usage (descending). The top process is the most CPU-intensive.
  2. If confirmed abnormal, press k, enter its PID (e.g., 1234), and press Enter to terminate (use sudo if admin privileges are required).

Scenario 2: Monitoring Memory Leaks

  • Press M to sort by memory usage (descending). The top process is the most memory-intensive.
  • If a process’s RES value continues to rise without falling, it may indicate a memory leak (investigate further, e.g., restart the service or analyze code).

Scenario 3: Resolving High System Load

  • Check the first line load average. If the 1-minute load > number of CPU cores (e.g., 4-core > 4), investigate:
  • Press P to find CPU-hungry processes. If wa (I/O wait) > 20%, check for slow I/O (e.g., replace HDD with SSD).
  • If us (user-space) is high, the program may be inefficient; optimize the code.

5. Conclusion

The top command is a foundational and practical “system dashboard” for Linux. It allows real-time monitoring of CPU, memory, and processes to quickly identify resource bottlenecks. For beginners, focus on core shortcuts: P (CPU sort), M (memory sort), 1 (multi-core), k (terminate), and q (quit). With repeated terminal practice, you’ll appreciate its power!

Xiaoye