
The top command in Linux is one of the most widely used tools for real-time system monitoring. It provides a dynamic overview of system performance, including resource usage by processes. Understanding the top command output is essential for system administrators to manage processes, optimize performance, and troubleshoot issues.
What is the Top Command in Linux?
The top command is a built-in utility in Unix-like operating systems that displays detailed information about system tasks and resource usage. It continuously updates the system’s active processes, providing administrators with insights into CPU, memory, and overall system performance.
How to Launch the Top Command
To execute the top command, simply open your terminal and type:
topThe output will display various statistics categorized into different sections. Each section provides crucial insights into the system’s current state.
Understanding the Top Command Output Layout
The top command output is divided into multiple parts, with each section offering different information about system performance. Below is a detailed explanation of the key sections:
1. Summary Information (Header Section)
The header section contains the following system-wide information:
- uptime: System running time since the last boot.
- users: Number of logged-in users.
- load average: The average system load over the last 1, 5, and 15 minutes.
- Tasks: Total number of processes and their states (running, sleeping, stopped, or zombie).
- CPU Usage: Percentage of CPU usage across different states:
- us: User processes
- sy: System processes
- ni: Nice value processes
- id: Idle CPU
- wa: I/O wait
- hi: Hardware interrupts
- si: Software interrupts
- st: Stolen time by virtual machines
Example:
Tasks: 150 total, 1 running, 149 sleeping, 0 stopped, 0 zombie
%Cpu(s): 3.4 us, 0.6 sy, 0.0 ni, 95.6 id, 0.2 wa, 0.0 hi, 0.2 si, 0.0 st2. Memory Usage
This section provides information about RAM and swap memory usage:
- Mem: Total memory, used memory, free memory, and buffers/cache memory.
- Swap: Total swap space, used swap, free swap, and cached memory.
Example:
MiB Mem : 7972.0 total, 3120.3 free, 3452.4 used, 1399.3 buff/cache
MiB Swap: 2048.0 total, 2048.0 free, 0.0 used. 3904.5 avail Mem3. Process Table (Task List)
The process table displays detailed information about individual processes. The columns in this section are:
- PID: Process ID
- USER: Owner of the process
- PR: Priority of the process
- NI: Nice value of the process
- VIRT: Virtual memory used by the process
- RES: Resident memory (RAM) used by the process
- SHR: Shared memory used by the process
- S: Process status (Running, Sleeping, Stopped, or Zombie)
- %CPU: CPU usage percentage
- %MEM: Memory usage percentage
- TIME+: Total CPU time consumed by the process
- COMMAND: Command name that started the process
Example:
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1234 root 20 0 500M 120M 10M S 0.3 1.5 0:02.15 apache2
5678 user1 20 0 400M 90M 8M R 1.2 1.1 0:05.32 chromeHow to Sort Processes in Top Command
The top command allows sorting processes by different parameters using the following keys:
- P: Sort by CPU usage
- M: Sort by Memory usage
- T: Sort by Time+ (CPU time)
- N: Sort by PID
To sort by memory usage, press Shift + M while the top command is running.
Interactive Top Command Options
The top command offers several interactive options to manipulate its output:
- k: Kill a process by entering its PID.
- r: Renice a process by changing its priority.
- q: Quit the top command.
- h: Display help information.
- d: Change update interval.
Customizing Top Command Output
You can customize the top command output to display only specific fields by pressing f and selecting the desired columns. Additionally, the top command allows switching between different display modes using the z key to toggle color highlighting.
Common Top Command Examples
- Show top 10 processes by memory usage:
top -o %MEM- Show top 10 processes by CPU usage:
top -o %CPU- Refresh every 2 seconds:
top -d 2
1- What is the use of the top command in Linux?
The top command is used to display real-time information about system performance, include CPU, memory, and process usage.
2- How can I sort processes by memory usage in top command?
Press Shift + M while the top command is running to sort processes by memory usage.
3- How to kill a process using the top command?
Press k, enter the PID of the process, and confirm by pressing Enter.
4- Can I customize the top command output?
Yes, press f to select which columns to display and z to toggle color highlighting
The top command in Linux is an essential tool for monitoring system performance and resource utilization. By understanding its output and interactive options, system administrators can optimize system performance, troubleshoot resource bottlenecks, and ensure efficient resource allocation. Mastering the top command allows better management of both system and application processes.