Linux top sort by mem

How to use top sort by cpu and by memory in linux

Linux top command plays a vital role in daily monitoring of server status.

This linux tutorial mainly shares with you how to sort the linux system CPU usage or memory usage when using the linux top command.

top sort by CPU in linux

A leading ‘+’ will force sorting high to low, whereas a ‘-” will ensure a low to high ordering.

top sort by memory in linux

top sort by pid in linux

top command supports sorted fields

Descriptions of fields

  • PID: Shows task’s unique process id.
  • USER: User name of owner of task.
  • PR: Stands for priority of the task.
  • NI: Represents a Nice Value of task. A Negative nice value implies higher priority, and positive Nice value means lower priority.
  • VIRT: Total virtual memory used by the task.
  • RES: Resident Memory Size (KiB).
  • SHR: Represents the Shared Memory size (kb) used by a task.
  • S : Process Status. The status of the task which can be one of:
    • D = uninterruptible sleep
    • I = idle
    • R = running
    • S = sleeping
    • T = stopped by job control signal
    • t = stopped by debugger during trace
    • Z = zombie
  • %CPU: Represents the CPU usage.
  • %MEM: Shows the Memory usage of task.
  • TIME+: CPU Time, the same as ‘TIME’, but reflecting more granularity through hundredths of a second.
  • COMMAND: Command Name or Command Line.

Best linux commands articles:

Источник

Find Top 15 Processes by Memory Usage with ‘top’ in Batch Mode

Similarly to the previous tip about find out top processes by RAM and CPU usage, you can also use top command to view the same information. Perhaps there’s an extra advantage of this approach when compared to the previous one: the “header” of top provides extra information about the current status and usage of the system: the uptime, load average, and total number of processes, to name a few examples.

Find Processes By Memory Usage with top

To display the top 15 processes sorted by memory use in descending order, do:

As opposed to the previous tip, here you have to use +%MEM (note the plus sign) to sort the output in descending order:

List Top 15 Processes By Memory Usage

From the command above, the option:

  1. -b : runs top in batch mode
  2. -o : used to specify fields for sorting processes
  3. head utility displays the first few lines of a file and
  4. the -n option is used to specify the number of lines to be displayed.

Note that head utility, by default displays the first ten lines of a file, that is when you do not specify the number of lines to be displayed. Therefore, in the example above, we displayed the first 22 lines of top command output in batch mode.

Redirect or Save ‘top’ Output to File in Linux

Additionally, using top in batch mode allows you to redirect the output to a file for later inspection:

Читайте также:  Docker daemon json windows

As we have seen, the top utility offers us more dynamic information while listing processes on a Linux system, therefore, this approach has an extra advantage compared to using ps utility which we covered in tip one.

But most importantly, you must always run top in batch mode to redirect its output to a file or another process. Additionally, if you have any tips regarding use of top, you can also share them with us via the feedback form below.

If You Appreciate What We Do Here On TecMint, You Should Consider:

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

We are thankful for your never ending support.

Источник

Check Memory Usage of Process with Linux Top Command

Generally, we look at the memory usage using the free command that provides us the total physical memory and used memory out of total memory. But top command is more useful to check memory usage in Linux. The free command display only physical memory usage but top display virtual memory usages by each process. It displays how much memory % a process is consuming separately for each running process.

Top command displays a listing of the most CPU utilization tasks and also can sort the tasks by CPU usage, memory usage and runtime.

Top sort by memory

Open shell to run top command, if we run top it will display only command name of the running process, to see full command we use -c option with top.

Then press m it will sort output with an order of highest memory usage at top as below:

Also you can run top command as following

Then press SHIFT + m from the keyboard to sort by memory usage.

Using shortcut keys

Modern Linux distro supports interactive menu for top to select memory usage.

Follow the below steps:

  • press Shift+f to enter the interactive menu.
  • press the up or down arrow until the %MEM choice is highlighted.
  • press s to select %MEM choice.
  • press enter to save your selection and exit ( q )the interactive menu.

Below screenshot shows the interactive menu Shift+f :

In older version of Linux it would be bit different as follows:

Run top command and press shift + f then the output will as displayed as shown below:

Now press the letter corresponding to %MEM ie ‘n’ and then press ‘ENTER’ key.

Using a single command

You can run the following single top command from the terminal to display memory usage:

Conclusion

In this tutorial we used top command to display active process usage ordered by memory usage. Linux also have ps and pmap commands to check memory usage.

If you have any questions or feedback, feel free to leave a comment.

Источник

15 simple TOP command examples on Linux to monitor processes

Linux TOP command

One of the most basic command to monitor processes on Linux is the top command. As the name suggests, it shows the top processes based on certain criterias like cpu usage or memory usage.

The processes are listed out in a list with multiple columns for details like process name, pid, user, cpu usage, memory usage.

Apart from the list of processes, the top command also shows brief stats about average system load, cpu usage and ram usage on the top.

This post shows you some very simple examples of how to use the top command to monitor processes on your linux machine or server.

Note your «top» command variant

Be aware that the top command comes in various variants and each has a slightly different set of options and method of usage.

To check your top command version and variant use the -v option

This post focuses on the top command coming from the procps-ng project. This is the version available on most modern distros like Ubunut, Fedora, CentOS etc.

1. Display processes

To get a glimpse of the running processes, just run the top command as is without any options like this.

Читайте также:  Образ загрузочной флешки windows 10 livecd

And immediately the output would be something like this —

The screen contains a lot of information about the system. The header areas include uptime, load average, cpu usage, memory usage data.

The process list shows all the processes with various process specific details in separate columns.
Some of the column names are pretty self explanatory.

PID — Process ID
USER — The system user account running the process.
%CPU — CPU usage by the process.
%MEM — Memory usage by the process
COMMAND — The command (executable file) of the process

2. Sort by Memory/Cpu/Process ID/Running Time

To find the process consuming the most cpu or memory, simply sort the list.

Press M key ( yes, in capital, not small ) to sort the process list by memory usage. Processes using the most memory are shown first and rest in order.

Here are other options to sort by CPU usage, Process ID and Running Time —

Press ‘P’ — to sort the process list by cpu usage.
Press ‘N’ — to sort the list by process id
Press ‘T’ — to sort by the running time.

3. Reverse the sorting order — ‘R’

By default the sorting is done in descending order. Pressing ‘R’ shall reverse the sorting order of the currently sorted column

Here is the output sorted in ascending order of cpu usage. Processes consuming the least amount of cpu are shown first.

4. Highlight the sorted column with bold text — ‘x’

Press x, to highlight the values in the sort column with bold text. Here is a screenshot, with the memory column in bold text —

top command highlight column

5. Highlight sorted column background color ‘b’

After highlighting the sorted column with bold font, its further possible to highlight with a different background color as well. This is how it looks

Top command highlight column background

6. Change the update delay — ‘d’

The top command updates the information on the screen every 3.0 seconds by default. This refresh interval can be changed.

Press the ‘d’ key, and top will ask you to enter the time interval between each refresh. You can enter numbers smaller than 1 second as well, like 0.5. Enter the desired interval and hit Enter.

7. Filter or Search processes — ‘o’/’O’

You can filter the process list based on various criterias like process name, memory usage, cpu usage etc. Multiple filter criterias can be applied.

Press the ‘o’ or ‘O’ to activate filter prompt. It will show a line indicating the filter format like this —

Then enter a filter like this and hit Enter.

COMMAND=apache

Now top will show only those processes whose COMMAND field contains the value apache.

Here is another filter example that shows processes consuming CPU actively —

%CPU>0.0

See active filters — Press Ctrl+o to see currently active filters

Clear filter — Press ‘=’ key to clear any active filters

8. Display full command path and arguments of process — ‘c’

Press ‘c’ to display the full command path along with the commandline arguments in the COMMAND column.

9. View processes of a user — ‘u’/’U’

To view the processes of a specific user only, press ‘u’ and then top will ask you to enter the username.

Which user (blank for all)
Enter the desired username and hit Enter.

10. Toggle the display of idle processes — ‘i’

Press ‘i’ to toggle the display of idle/sleeping processes. By default all processes are display.

11. Hide/Show the information on top — ‘l’, ‘t’, ‘m’

The ‘l’ key would hide the load average information.
The ‘m’ key will hide the memory information.
The ‘t’ key would hide the task and cpu information.

Hiding the header information area, makes more processes visible in the list.

12. Forest mode — ‘V’

Pressing ‘V’ will display the processes in a parent child hierarchy. It looks something like this —

13. Change the number of processes to display — ‘n’

Lets say you want to monitor only few processes based on a certain filter criteria. Press ‘n’ and enter the number of processes you wish to display.

It will display a line saying —
Maximum tasks = 0, change to (0 is unlimited)

Читайте также:  Dual boot windows with grub

14. Display all CPU cores — ‘1’

Pressing ‘1’ will display the load information about individual cpu cores. Here is how it looks —

15. Show/Hide columns ‘f’

By default top displays only few columns out of many more that it can display. If you want to add or remove a particular column or change the order of columns, then press f

The fields marked * or bold are the fields that are displayed, in the order in which they appear in this list.

Navigate the list using up/down arrow keys and press ‘d’ to toggle the display of that field. Once done, press q to go back to the process list

The following output displays only PID, USER, CPU, MEMORY and COMMAND columns.

16. Batch mode

Top also supports batch mode output, where it would keep printing information sequentially instead of a single screen. This is useful when you need to log the top output for later analysis of some kind.

Here is a simple example that shows the Cpu usage at intervals of 1 second.

17. Split output in multiple panels — ‘A’

Each panel can be sorted on a different column. Press ‘a’ to move through the panels. Each panel can have a different set of fields displayed and different sort columns.

top command multiple panels

Conclusion

Top is a great commandline tool to check system resource usage and process details. Its often used on servers for monitoring and quick diagnosis.

If you are looking for something easier with a better user interface then try htop. Htop has a intuitive user interface, where you need not memorize keyboard shortcuts. Htop has onscreen instructions that guide you on how to use it.

A Tech Enthusiast, Blogger, Linux Fan and a Software Developer. Writes about Computer hardware, Linux and Open Source software and coding in Python, Php and Javascript. He can be reached at [email protected] .

10 thoughts on “ 15 simple TOP command examples on Linux to monitor processes ”

Nice article and great explanation.

if same user id is running multiple times how to delete all others except one.Please any one reply me fast.
exp
PID USER PR %cpu
QPD02201
QPD02201
QPD02201
QPD02201

A really top article!

Hi, thanks for the great article but I’m still looking for an answer for my case: It looks like that all the time. CPU load is around 45-50% all the time but if I add up the CPU usage of the services it never gets close… Why’s that?

Tasks: 251 total, 1 running, 250 sleeping, 0 stopped, 0 zombie
%Cpu0 : 50,1 us, 10,1 sy, 0,0 ni, 39,8 id, 0,0 wa, 0,0 hi, 0,0 si, 0,0 st
%Cpu1 : 55,7 us, 7,2 sy, 0,0 ni, 36,8 id, 0,0 wa, 0,0 hi, 0,2 si, 0,0 st
%Cpu2 : 35,6 us, 4,4 sy, 0,0 ni, 58,8 id, 0,0 wa, 0,0 hi, 1,2 si, 0,0 st
%Cpu3 : 36,7 us, 5,8 sy, 0,0 ni, 56,9 id, 0,0 wa, 0,0 hi, 0,6 si, 0,0 st
KiB Mem : 16242248 total, 12711832 free, 2071424 used, 1458992 buff/cache
KiB Swap: 3809276 total, 3809276 free, 0 used. 13807884 avail Mem

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
9066 root 20 0 830468 35332 25632 S 2,2 0,2 2:19.18 metricbeat
5428 www-data 20 0 72168 27716 2520 S 1,2 0,2 3:28.49 kix.Daemon.pl
590 kopano 20 0 291488 41016 24640 S 0,6 0,3 1:27.52 kopano-search
581 kopano 20 0 355280 38644 18984 S 0,4 0,2 1:27.69 kopano-presence
7 root 20 0 0 0 0 S 0,2 0,0 0:31.24 rcu_sched
580 root 20 0 250116 4332 2508 S 0,2 0,0 0:01.37 rsyslogd
821 postgres 20 0 268376 6448 4688 S 0,2 0,0 0:00.07 postgres
1569 mysql 20 0 739704 114760 17024 S 0,2 0,7 0:38.78 mysqld
1802 root 20 0 392128 12028 6828 S 0,2 0,1 0:08.35 docker-containe
1875 root 20 0 3125288 23428 9428 S 0,2 0,1 0:40.27 slapd
2043 root 20 0 818760 70340 28176 S 0,2 0,4 0:37.41 named
2686 kdm 20 0 392288 59108 33904 S 0,2 0,4 0:29.23 kdm_greet
2871 root 20 0 4822492 437276 23464 S 0,2 2,7 2:01.74 java
9560 root 20 0 588668 69552 22952 S 0,2 0,4 0:40.12 packetbeat
10650 root 20 0 38144 3588 3012 R 0,2 0,0 0:04.37 top

Maybe it looks better like this

%Cpu0 : 50,1 us, 10,1 sy,
%Cpu1 : 55,7 us, 7,2 sy,
%Cpu2 : 35,6 us, 4,4 sy,
%Cpu3 : 36,7 us, 5,8 sy,

Источник

Оцените статью