Using the 'top' Command (with examples)

Using the 'top' Command (with examples)

  • Osx
  • November 5, 2023

The top command is a powerful tool that allows you to view dynamic real-time information about running processes on your system. It provides a wealth of information, such as CPU usage, memory usage, and process details. In this article, we will explore several use cases of the top command and explain the various options available.

Use Case 1: Display All Processes (Default Order)

To start top and display all processes with their default ordering (process ID), simply enter the following command:

top

Motivation: This use case is useful when you want to quickly view all running processes in real-time. It provides an overview of the system’s workload and helps identify resource-intensive processes.

Example Output:

Processes: 301 total, 2 running, 299 sleeping, 1594 threads
Load Avg: 2.03, 2.32, 2.55  CPU usage: 12.45% user, 19.60% sys, 68.95% idle
SharedLibs: 209M resident, 60M data, 17M linkedit.
MemRegions: 57438 total, 3577M resident, 95M private, 718M shared.
PhysMem: 16G used (4196M wired), 347M unused.

Use Case 2: Sort Processes by Internal Memory Size

By default, top sorts processes by their process ID. However, you can specify a different sorting order. To sort processes by internal memory size (from largest to smallest), use the -o option followed by the mem argument:

top -o mem

Motivation: Sorting processes by memory usage helps identify processes that may be consuming excessive memory resources. This information is valuable for troubleshooting memory-related issues or optimizing system performance.

Example Output:

Processes: 301 total, 2 running, 299 sleeping, 1594 threads
Load Avg: 2.03, 2.32, 2.55  CPU usage: 12.45% user, 19.60% sys, 68.95% idle
SharedLibs: 209M resident, 60M data, 17M linkedit.
MemRegions: 57438 total, 3577M resident, 95M private, 718M shared.
PhysMem: 16G used (4196M wired), 347M unused.

PID    COMMAND          %CPU  TIME     #TH    #WQ   MEM        COMMAND
100    Safari           12.4  01:23:45 10     5     1757M      Safari
567    Chrome           8.8   00:15:40 5      2     1125M      Chrome
456    Firefox          5.5   00:10:12 3      1     857M       Firefox
...

Use Case 3: Sort Processes by CPU and Running Time

To sort processes first by CPU usage (from highest to lowest) and then by running time (from longest to shortest), use the -o option followed by the cpu argument and the -O option followed by the time argument:

top -o cpu -O time

Motivation: This use case helps identify processes that are consuming the highest CPU resources and have been running for an extended period. It provides insights into CPU-intensive processes and aids in troubleshooting performance bottlenecks.

Example Output:

Processes: 301 total, 2 running, 299 sleeping, 1594 threads
Load Avg: 2.03, 2.32, 2.55  CPU usage: 12.45% user, 19.60% sys, 68.95% idle
SharedLibs: 209M resident, 60M data, 17M linkedit.
MemRegions: 57438 total, 3577M resident, 95M private, 718M shared.
PhysMem: 16G used (4196M wired), 347M unused.

PID    COMMAND          %CPU  TIME     #TH    #WQ   MEM        COMMAND
124    Xcode            36.5  01:23:45 27     12    985M       Xcode
567    Chrome           14.2  00:15:40 5      2     1125M      Chrome
100    Safari           8.8   01:00:00 10     5     1757M      Safari
...

Use Case 4: Display Processes Owned by a User

If you want to view only the processes owned by a specific user, you can use the -user option followed by the user’s name:

top -user user_name

Motivation: Sometimes, you may want to focus on the processes belonging to a particular user, especially in multi-user systems. This use case allows you to filter the information and analyze the workload of a specific user.

Example Output:

Processes: 301 total, 2 running, 299 sleeping, 1594 threads
Load Avg: 2.03, 2.32, 2.55  CPU usage: 12.45% user, 19.60% sys, 68.95% idle
SharedLibs: 209M resident, 60M data, 17M linkedit.
MemRegions: 57438 total, 3577M resident, 95M private, 718M shared.
PhysMem: 16G used (4196M wired), 347M unused.

PID    COMMAND          %CPU  TIME     #TH    #WQ   MEM        COMMAND
100    Safari           12.4  01:23:45 10     5     1757M      Safari
101    Mail             8.9   01:10:10 6      3     750M       Mail
...

Use Case 5: Get Help with Interactive Commands

The top command provides a set of interactive commands that allow you to customize the display and control the behavior of the command. To access the help menu and view all available interactive commands, simply press the ? key while using top.

?

Motivation: Understanding the available interactive commands helps you navigate and interact with the top command effectively. It provides shortcuts and additional information that enhances your ability to analyze the running processes.

Example Output:

Interactive commands:  ('?' or 'h' for help)  (use the arrow keys to navigate)

  P :           Sort by CPU usage
  M :           Sort by memory usage
  O :           Change the sort order (ascending / descending)
  H :           Hide/show user and kernel threads
  I :           Invert sort order
  R :           Reverse the sort order temporarily
  W :           Highlight the currently selected column
...

In this article, we explored several useful examples of using the top command. We learned how to display all processes, sort processes by different criteria, filter processes by user, and access the interactive commands. With these examples, you can leverage the power of the top command to monitor and analyze running processes on your system efficiently.

Tags :

Related Posts

How to use the command 'size' (with examples)

How to use the command 'size' (with examples)

The size command is a tool for displaying the sizes of sections inside binary files.

Read More
How to use the command 'adduser' (with examples)

How to use the command 'adduser' (with examples)

The ‘adduser’ command is a user addition utility that allows users to create new users and specify various options such as the home directory, password, shell, and group for the new user.

Read More
How to use the command xclip (with examples)

How to use the command xclip (with examples)

The command xclip is a tool for manipulating the clipboard in the X11 window system.

Read More