Linux Commands : top command explained

By Yashwant
In Linux
Feb 24th, 2014
1 Comment
2398 Views

Recently I got a chance to work on improving performance of development machines and I learnt a few more linux commands related with system administration. We all love faster machines yet keep on cribbing about how slow our machines work. This time I tried checking why exactly our machines are slow and what are various tools which can tell us what’s exactly is making our machine slower. Apart from tools there are some good practices which can make make our machines work faster and our lives easier(!) I will write those things as part of this series in due course of time.

Linux Commands : top command explained

Linux Commands : top command explained

Every machine has resources like RAM and CPU which are used by all the processes running on that machine. If a machine is running slow, it simply means there is some process which is using resources extensively. Linux provide some very good built in tools which help us understanding what’s exactly is using our machine resources. In this article I will talk specifically about top command which is a great tool to analyze usage of resource and best part is it keeps on updating the data in real time.

Syntax # top

Linux Commands : top command explained

Linux Commands : top command explained

Scenario – I have a real time scenario on my Linux system. I am running an Oracle DB, a standalone Weblogic Server and a JDeveloper. In general these three applications are the ones with which a general ADF developer and his machine spends most of the time. Here is a screenshot which shows the output of top command on my machine

top - 22:20:44 up 13 days, 7:16, 9 users, load average: 1.25, 0.44, 0.44
Tasks: 244 total, 1 running, 242 sleeping, 0 stopped, 1 zombie
Cpu(s): 5.2%us, 0.3%sy, 0.0%ni, 94.0%id, 0.3%wa, 0.0%hi, 0.2%si, 0.0%st
Mem: 8107652k total, 7928448k used, 179204k free, 689768k buffers
Swap: 2096440k total, 128k used, 2096312k free, 4791912k cached
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
6577 ygolecha 18 0 2363m 654m 5992 S 6.3 8.3 0:34.54 java
21761 oradba 18 0 751m 346m 333m S 0.0 4.4 11:09.86 oracle
21728 oradba 15 0 745m 288m 282m S 0.0 3.6 0:10.70 oracle
21732 oradba 15 0 746m 287m 281m S 0.0 3.6 0:58.99 oracle
4214 ygolecha 25 0 1686m 249m 21m S 0.0 3.1 0:18.07 java
21722 oradba 15 0 748m 218m 206m S 0.0 2.8 0:02.60 oracle
4175 ygolecha 15 0 685m 206m 17m S 0.0 2.6 1:27.21 gedit
13175 ygolecha 25 0 1199m 77m 6084 S 0.0 1.0 0:24.43 java

If you see initial 5 lines, it depicts a lot of info about your machine. Uptime, Total No of Process, CPU, RAM, Swap. All general info in the beginning but the magic starts after that. The following table with various columns is quite interesting. Columns which we are more interested in are RES, %CPU and %MEM. RES is amount of RAM used by that process at that time.

If you execute top command on your terminal, you can see data updating continuously. If you compare the same with the screenshot in this post, you will see a difference. In my output, data is sorted in order of highest memory consuming processes.

To sort processes in order of memory consumption – press M

One very useful feature is that you can kill any process which you think is unnecessary from the top command terminal itself. Simply press ’k’ and then give process id which you want to kill and hit enter. The process will be killed and you can see updated data in the table.

Sorting data on memory consumption and killing unnecessary process directly are two things which are most useful in our day to day use cases. For detailed infor about more use cases of top, check following link

Can You Top This? 15 Practical Linux Top Command Examples

If you have noticed the table, you will see 2 java processes and few oracle processes. Where is our Weblogic Server and JDeveloper?? We have always believed these two are the processes which eat up all the memory. Well, don’t be surprised, the java processes shown in the table represent them only. In general terms I can say both JDeveloper and Weblogic server are java applications only so they will be shown as java processes.

If we check from parent child process perspective, things will become a bit more clear. Try ps -a on your terminal and check the process id related with jdev and startWeblogic.sh. There will be two processes running with each name and you will be wondering why is it so? Don’t worry, your machine is not running two JDevelopers (I know from my personal experience that running two JDevelopers in one machine is a nightmare for the machine). It’s a hierarchy of processes in most cases in linux.

Type pstree ppid (ppid – parent process id) and hit enter. You can see the whole tree. (Try pstree -c ppid for more details )

You can kill either the java process or use pkill -TERM -P ppid

Linux Commands : ps command explained

Linux Commands : ps command explained

So go ahead and start exploring more about these commands. Stay tuned for more updates.

Happy Coding 🙂

About the Author

- An avid reader and a big time comics fan. A software engineer by profession who loves writing code and make things work.

One Response to “Linux Commands : top command explained”

  1. […] explained top command in my previous post regarding improving performance of linux systems. When we talk about […]

Leave a Reply

Your email address will not be published. Required fields are marked *

facebook comments: