A technical troubleshooting blog about Oracle with other Databases & Cloud Technologies.

How to Use the top Command in Linux ??

4 min read
The top utility is a commonly used tool for displaying system-performance information. It dynamically shows administrators which processes are consuming processor and memory resources. Top is incredibly handy.

The top (table of processes) command shows a real-time view of running processes in Linux and displays kernel-managed tasks. The command also provides a system information summary that shows resource utilization, including CPU and memory usage.
# top

Press the " f " key and scroll down using the <down> arrow key then select "SWAP" then press <Space> to select it. This should add a " * " symbol in front of it.

While still selecting "SWAP"  press the <right> arrow key, which highlights the entire SWAP line, and using the <top> arrow key move it up to one of the first options (anywhere above "COMMAND").

While still having "SWAP" selected, type the " s " key which will configure top to SORT by the currently selected option, in this case SWAP.

Finally " q " to save the configuration changes and view the results.

Perform a review as needed and press "q" again to exit top command.
[oracle@<HOST> ]$ top

top - 05:29:18 up 254 days, 13:27, 10 users, load average: 0.80, 0.88, 1.44
Tasks: 563 total, 1 running, 560 sleeping, 0 stopped, 2 zombie
Cpu(s): 0.5%us, 0.3%sy, 0.0%ni, 97.9%id, 1.2%wa, 0.0%hi, 0.0%si, 0.0%st
Mem: 103302740k total, 81192632k used, 22110108k free, 764272k buffers
Swap: 36700156k total, 329780k used, 36370376k free, 22772280k cached

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND

50651 oracle 20 0 29.8g 38m 30m S 1.0 0.0 8:59.40 oracle<DBNAME> (LOCAL=NO)
8392 root 20 0 110m 2556 1896 S 0.7 0.0 534:25.24 /usr/bin/Xorg :0 -br -audit 0 -auth /var/gdm/:0.Xauth -nolisten tcp vt7
8687 grid 20 0 433m 30m 12m S 0.7 0.0 1597:27 /u01/app/grid/product/11.2.0/grid/bin/oraagent.bin
# man top


MEM -- Memory Usage (RES)
A task's currently used share of available physical memory.

RES -- Resident Memory Size (KiB)
The non-swapped physical memory a task is using.

SHR -- Shared Memory Size (KiB)
The amount of shared memory available to a task, not all of which is typically resident. It simply reflects memory that could be potentially shared with other processes.

SWAP -- Swapped Size (KiB)
The non-resident portion of a task's address space.

VIRT -- Virtual Memory Size (KiB)
The total amount of virtual memory used by the task. It includes all code, data and shared libraries plus pages that have been swapped out and pages that have been mapped but not used.
The first five lines displayed at the top :

* Displays uptime information
* Tasks displays process status information
* %Cpu(s) displays various processor values
* MiB Mem displays physical memory utilization
* MiB Swap displays virtual memory utilization
The first value is the system time. The second value represents how long the system has been up and running, while the third value indicates the current number of users on the system. The final values are the load average for the system.

The load average is broken down into three time increments. 

The first shows the load for the last one minute, the second for the last five minutes, and the final value for the last 15 minutes. 

The results are a percentage of CPU load between 0 and 1.0. The processor is likely overworked if 1.0 (or higher) is displayed.
top - 11:09:27 up 10 min, 1 user, load average: 0.45, 0.42, 0.11
To quit the top function, press the letter q on your keyboard.

Some other useful commands while top is running include:

M – sort task list by memory usage
P – sort task list by processor usage
N – sort task list by process ID
T – sort task list by run time
The second line is the Tasks output, and it's broken down into five states. 

* Total -> sum of the processes from any state.
* Running -> how many processes are handling requests, executing normally, and have CPU access.
* Sleeping -> processes awaiting resources, which is a normal state.
* Stopped -> processes exiting and releasing resources; these send a termination message to the parent process.

* Zombie :

 A zombie process, also known as a defunct process, is a terminated process that has not been fully removed from the process table. 

It exists in this state until the parent process acknowledges its termination and collects its exit status. Zombie processes consume minimal system resources but still occupy an entry in the process table.

The zombie process consumes minimal system resources, as it only occupies an entry in the process table and a small amount of memory. However, it does not actively consume CPU resources.
Tasks: 670 total, 7 running, 498 sleeping, 0 stopped, 0 zombie
Third line represents processor utilization . CPU Utilization is validated:

* us is the percent of time spent running user processes.
* sy is the percent of time spent running the kernel.
* ni is the percent of time spent running processes with manually configured nice values.
* id is the percent of time idle (if high, CPU may be overworked).
* wa is the percent of wait time (if high, CPU is waiting for I/O access).
* hi is the percent of time managing hardware interrupts.
* si is the percent of time managing software interrupts.
* st is the percent of virtual CPU time waiting for access to physical CPU.

Values such as id, wa, and st help identify whether the system is overworked.
%Cpu(s): 29.9 us, 4.0 sy, 0.0 ni, 84.2 id, 0.0 wa, 0.6 hi, 6.7 si, 0.0 st
The top utility reports memory consumption in decimal.

* Total -> total installed memory.
* Free -> available memory.
* Used -> consumed memory.
* Buffer/cache -> the amount of information buffered to be written.