how to sort the output of 'top' command by memory or cpu usage

One of the programs that you can use to quickly see the various process information in Linux is the top command. The fact that the command can be run from virtually any command prompt and it is self updating makes it an extremely handy tool. This allows you can quickly find the processes that are consuming too many resources on the machine.

In most cases, running the top command without any command line options is good enough to get the necessary information about the running processes. But there are some command line options that can be useful, one of which is the option to override the default sort field.

You can use the -o command line option followed by the field name to sort the output by a particular stat or field. The field format supports the ability to specify the order of sort as well. You can use the '+' before the field name to specify a high to low sort, while '-' can be used to specify the low to high sorting order.

sort by memory usage

So, if you want to sort the output by the memory used by each of the processes, you specify the %MEM field as the field to be sorted. Actually, %MEM stands for percentage of memory, so you are sorting by percent. You could use RES or VIRT fields, if you would like to sort by another memory field.

$ top -o +%MEM

The above command will sort the table by the MEM (memory) field. The MEM field displays the current resident share of the task or process with respect to the total available physical memory on the machine. The '+'  that precedes the field name (%MEM) specifies that the field will be sorted in descending order. The tasks that is eating the most memory will be displayed on top of the table.

sort by CPU usage

In order to sort by the CPU usage of the processes or tasks, you use the %CPU field just as in the example above.

$ top -o +%CPU

The %CPU field displays the share of CPU time used by the task since the last update. The field is shown as a percentage of the total CPU time.

linux-top-sort-by-cpu.png

The memory and the CPU are not the only fields you can sort by. They are usually the most useful and the often used fields. You can use any of the fields in the output for sorting purposes. So, if you want to sort the output of top to display the programs that have used the most CPU, you can use the TIME field to do that….

$ top -o +TIME

Similarly, you can use PR for priority, PID for process id, VIRT for virtual memory etc.

Filtering the output of top

Now, you could restrict the processes or tasks that are displayed as well. For example, you might want to get only the top 10 tasks that is using the most memory. In this case, you can filter using the head utility to display only the first 10 lines of the table.

$ top -b -o +%MEM | head -n 17

You will need to use the -b or the batch mode of the top command, so that you can filter the output using the head utility. Also, you must have noticed that the first 7 lines are used as summary by the top command, which means will need to print out the first 17 lines to get the top 10 processes.

If you do not want to display the summary table on top, but just the process table with the top 10 tasks, then you can use the sed utility to display only specific lines.

$ top -b -o +%MEM | sed -n '7,17p'

The above will print out just the line 7 through 17 which is the top 10 tasks sorted by the memory usage.