How can I display the top results in my terminal in real time so that the list is sorted by memory usage?
- 141
- 5,851
10 Answers
Use the top command in Linux/Unix:
top
- press shift+m after running the
topcommand - or you can interactively choose which column to sort on
- press Shift+f to enter the interactive menu
- press the up or down arrow until the
%MEMchoice is highlighted - press s to select
%MEMchoice - press enter to save your selection
- press q to exit the interactive menu
Or specify the sort order on the command line
# on OS-X
top -o MEM
# other distros
top -o %MEM
References
https://stackoverflow.com/questions/4802481/how-to-see-top-processes-by-actual-memory-usage
-
7
-
@GabrielHautclocq It must depend upon your distribution of Linux, and the package bundled with it. Debian 7 uses
procps-ngand there is no-ooption at all in that version.SHIFT-Mworks for me oncetopis launched. – Christopher Schultz Sep 01 '17 at 13:59 -
top -o %MEMworks on my debian 8 and 9, but not on debian 7, you are right @Christopher Schultz. – Gabriel Hautclocq Sep 03 '17 at 17:16 -
-
-
use
top -Oto get a list of the field names which could be used for that -o argument – charlie arehart Jun 04 '22 at 17:29 -
The command line option -o (o standing for "Override-sort-field") also works on my Xubuntu machine and according to the Mac man page of top it should work on a Macintosh too. If I want to short by memory usage I usually use
top -o %MEM
which sorts by the column %MEM. But I can use VIRT, RES or SHR too. On a Macintosh I would probably use mem or vsize.
I don't know why or how but this is pretty much different between Unix systems and even between Linux distributions. For example -o isn't even available on my Raspberry running Wheezy. It may be worth give it a try though.
- 1,357
-
3The answer could user more clarity:
%MEMis given as an answer to the eager reader; while it doesn't work everywhere (by far). – 7heo.tk May 06 '15 at 15:00 -
2For Macbook 2014 this is saying:
top -o %MEM invalid argument -o: %MEM– fIwJlxSzApHEZIl Jun 01 '15 at 17:43 -
-
-
-
2@anon58192932 you should replace
%MEM(orPID,VIRT, etc.) by any column name that you see when runningtoponly. As noted by ytg, "on a Macintosh I would probably usememorvsize". – ebosi Mar 29 '17 at 01:59 -
or
cpufrom mac's top -h:[-o <key>] [-O <secondaryKey>] keys: pid (default), command, cpu, cpu_me, cpu_others, csw, time, threads, ports, mregion, mem, rprvt, purg, vsize, vprvt, kprvt, kshrd, pgrp, ppid, state, uid, wq, faults, cow, user, msgsent, msgrecv, sysbsd, sysmach, pageins, boosts, instrs, cycles– alexey Feb 05 '18 at 19:53 -
+1 to the preceding comment. On my Mac (10.13.6),
top -o '%CPU'did not work, notwithstanding that "%CPU" is how the column header appears.top -o CPUworked fine, as didtop -o cpu. – AbuNassar Aug 21 '18 at 14:23 -
1
-
use
top -Oto get a list of the field names which could be used for that -o argument – charlie arehart Jun 04 '22 at 17:30 -
top isn't a POSIX utility. Even POSIX utilities still only have a few standard options, but implementations will introduce their own extensions. Don't you see BSD
findbeing vastly different from GNUfindor busyboxfind? Depending on which userspace tool the distro uses you'll have different syntax – phuclv Aug 03 '22 at 05:41
For Ubuntu 14.04 starting with
htop -s PERCENT_MEM
or (equivalently)
htop --sort-key PERCENT_MEM
did the trick for me.
-
-
4it's different command. Looking for answer about the
topcommand (as asked in this question) nothtop. – Lukas Liesis Oct 28 '18 at 09:55 -
htopis obviously a completely different tool in a different package. In lots of cases you have no choice buttopbecause there's nohtopto install – phuclv Aug 03 '22 at 04:31
It seems like the -o flag will take the actual column name. So if the top command shows only "mem" then the command should be "top -o mem".
For the ubuntu machine I am testing with, the column is called "%MEM". On the OSX Yosemite I tried, it is "mem".
- 159
-
use
top -Oto get a list of the field names which could be used for that -o argument – charlie arehart Jun 04 '22 at 17:30
The original question seems to have been for a Mac, but for anyone else stumbling across this answer, on Red Hat Linux (and many others), 'top -m' starts top with results sorted by memory usage.
- 81
-
-
1Worked on: Red Hat Enterprise Linux Server release 6.8 (none of the other answers worked). – Contango Mar 21 '17 at 17:18
If you're using the top that comes with Ubuntu (top -v = procps-ng version 3.3.10), then you can use these interactive keyboard shortcuts to change the sorting. Note that these are all capital letters, so either use shift or caps lock.
M %MEM
N PID
P %CPU
T TIME+
By default, they will be sorted in DESC order. Use R to toggle ASC/DESC.
To set the sorting from the command line option, use top -o %MEM. You can specify any column.
- 3,388
If top is already running, press o . Above the data, a prompt will appear:
primary key [xxxxx]:
Where xxxxx is the current sorting key. Type the name of the column by which you want to sort. If a column name contains "%" or "#", omit the character. For %CPU, just type "cpu".
- 5,941
On RHEL 7 & 8, after running top I just type > to move across columns to sort by.
Since it starts sorted by CPU, only one > is required.
A commenter on the original question has also suggested this.
- 72,889
- 111
>and<move the sort column right and left. Since the%MEMcolumn is just right of the%CPUcolumn, which is also the default sort column, it takes only one keystroke to switch between the two. I know, your question has the macintosh tag, that's why I'm writing this answer as a comment. – Walter Tross Sep 19 '15 at 18:37htop, mainly because it tells me how to do this. – lindhe Jan 19 '16 at 22:05htop, in addition toshift + M, you will likely want to turn off the display of threads and just show the main process memory consumption withshift + H. See https://unix.stackexchange.com/a/10403/27902. – Elijah Lynn May 11 '22 at 00:10