591

How can I display the top results in my terminal in real time so that the list is sorted by memory usage?

3
  • 48
    On Linux, > and < move the sort column right and left. Since the %MEM column is just right of the %CPU column, 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. Commented Sep 19, 2015 at 18:37
  • 18
    I prefer htop, mainly because it tells me how to do this.
    – lindhe
    Commented Jan 19, 2016 at 22:05
  • 5
    If using htop, in addition to shift + M, you will likely want to turn off the display of threads and just show the main process memory consumption with shift + H. See unix.stackexchange.com/a/10403/27902. Commented May 11, 2022 at 0:10

10 Answers 10

673

Use the top command in Linux/Unix:

top
  • press shift+m after running the top command
  • 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 %MEM choice is highlighted
    • press s to select %MEM choice
    • 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
  • 9
    On Linux it's top -o %MEM (note the "%") Commented Sep 1, 2017 at 9:19
  • @GabrielHautclocq It must depend upon your distribution of Linux, and the package bundled with it. Debian 7 uses procps-ng and there is no -o option at all in that version. SHIFT-M works for me once top is launched. Commented Sep 1, 2017 at 13:59
  • top -o %MEM works on my debian 8 and 9, but not on debian 7, you are right @Christopher Schultz. Commented Sep 3, 2017 at 17:16
  • instead of <key>enter</key> it should be <key>q</key>
    – dashesy
    Commented Jul 15, 2019 at 19:23
  • "Press Shift + m" is a long-winded way of saying "type M".
    – Blaine
    Commented Aug 9, 2020 at 19:47
125

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.

11
  • 3
    The answer could user more clarity: %MEM is given as an answer to the eager reader; while it doesn't work everywhere (by far).
    – 7heo.tk
    Commented May 6, 2015 at 15:00
  • 2
    For Macbook 2014 this is saying: top -o %MEM invalid argument -o: %MEM Commented Jun 1, 2015 at 17:43
  • I'm sorry to hear that.
    – ytg
    Commented Jun 1, 2015 at 20:03
  • 2
    @anon58192932 you should replace %MEM (or PID, VIRT, etc.) by any column name that you see when running top only. As noted by ytg, "on a Macintosh I would probably use mem or vsize".
    – ebosi
    Commented Mar 29, 2017 at 1:59
  • 1
    Yep, just do top -o MEM since "MEM" is on the list.
    – devinbost
    Commented Jul 30, 2021 at 18:37
26

For Ubuntu 14.04 starting with

htop -s PERCENT_MEM

or (equivalently)

htop --sort-key PERCENT_MEM

did the trick for me.

3
  • Works well for OSX as well.
    – ehime
    Commented May 2, 2016 at 18:16
  • 4
    it's different command. Looking for answer about the top command (as asked in this question) not htop. Commented Oct 28, 2018 at 9:55
  • htop is obviously a completely different tool in a different package. In lots of cases you have no choice but top because there's no htop to install
    – phuclv
    Commented Aug 3, 2022 at 4:31
15

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".

1
  • use top -O to get a list of the field names which could be used for that -o argument Commented Jun 4, 2022 at 17:30
9

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.

8

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.

2
  • Not on Debian 8 Commented Feb 27, 2017 at 13:45
  • 1
    Worked on: Red Hat Enterprise Linux Server release 6.8 (none of the other answers worked).
    – Contango
    Commented Mar 21, 2017 at 17:18
7

Ubuntu 14.04 - this works just fine:

htop --sort-key=PERCENT_MEM
1
  • 5
    htop is not top
    – Anthon
    Commented Aug 15, 2016 at 9:28
7

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".

7

On linux, run:

$ top

Then press, Shift + M.

0

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .