288

Sometimes it is not comfortable to see meminfo in kilobytes when you have several gigs of RAM. In Linux, it looks like:

top, with memory stats all scaled to Kb

And here is how it looks in Mac OS X:

top, with memory stats scaled to Mb and Gb

Is there a way to display meminfo in Linux top in terabytes, gigabytes and megabytes?

6
  • 9
    Apparently, some posts say that in Redhat Linux you can do someting like top -M to display the usage in MB. If you only want to monitor the memory usage, you can use rather use htop. Not sure of any other option.
    – Barun
    Dec 19, 2013 at 15:54
  • Right you are, but there is nothing about that in build-in help. I've just occasionally was able to find that in man page Dec 19, 2013 at 15:56
  • 2
    The man page is the builtin help.
    – casey
    Dec 19, 2013 at 15:59
  • 7
    You could always use free -m, or better free -h instead.
    – terdon
    Dec 19, 2013 at 16:48
  • 8
    Once you jump into top hit E until it shows the memory cumulative you're looking for, then hit W to write that configuration to disk. Apr 13, 2015 at 2:07

9 Answers 9

354

When in top, typing capital "E" cycles through different memory units (KiB, MiB, GiB, etc., which are different from kB, MB and GB) in the total memory info:

The image shows tops general memory display with GiB as a unit.

While lower-case "e" does the same individual process lines:

The image shows processes in top where the memory is displayed in MiB

From the manpage:

2c. MEMORY Usage
    This  portion  consists of two lines which may express values in kibibytes
    (KiB) through exbibytes (EiB) depending on  the  scaling  factor  enforced
    with the 'E' interactive command.

Version Information: top -version: procps-ng version 3.3.9
System: CentOS 7

8
  • 11
    Does not work for me in RHEL May 11, 2014 at 11:49
  • 5
    Works in ubuntu 14.04 cheers.
    – tjjjohnson
    May 22, 2014 at 4:22
  • works in fedora 20
    – sivann
    Sep 19, 2014 at 8:48
  • 6
    @AnthonyAnanich: Does work for me in RHEL 7.
    – krlmlr
    Dec 17, 2014 at 11:14
  • 7
    <time machine from the future>I find the default units of Petabytes is woefully inadequate and unhelpful. I always end up having to hit E at least once to set top in units of Exabytes.</time machine from the future> Feb 16, 2017 at 15:12
86

There is a command-line option which does that:

-M : Detect memory units
            Show memory units (k/M/G) and display floating point values in the
            memory summary.

So it is sufficient to run top like that:

top -M

If -M does not work you can press E while already in top.

From man top (procps-ng version 3.3.9):

E :Extend-Memory-Scale in Summary Area With this command you can cycle through the available summary area memory scaling which ranges from KiB (kibibytes or 1,024 bytes) through EiB (exbibytes or 1,152,921,504,606,846,976 bytes).

If you see a '+' between a displayed number and the following label, it means that top was forced to truncate some portion of that number. By raising the scaling factor, such truncation can be avoided.

top, showing Memory units in "M"

6
  • 28
    +1 , but checked in Ubuntu , its saying not working. Any way in Ubuntu ?
    – Raja G
    Dec 19, 2013 at 15:59
  • I have no clue, @richardparker Dec 19, 2013 at 16:32
  • 2
    Damn, you racked up a ton of karma with a simple (but good) self-answered Q&A. Nice work. Oct 29, 2015 at 15:39
  • 4
    That blur isn't blurry enough, antonio. May 24, 2016 at 9:19
  • 3
    Works for RHEL 6 (pressing 'E' does not as the top comment)
    – Molten Ice
    Sep 9, 2016 at 10:02
64

You can also use htop. It's much cooler than top.

If you are using Debian or one of its derivatives, then you can install it using sudo apt-get install htop.

htop screenshot

Edit: Here is a screenshot with a better color scheme:

htop screenshot with better colors

5
  • 22
    htop looks better with either a white or a black background. Your screenshot color scheme is less than optimal.
    – jlliagre
    Dec 19, 2013 at 17:42
  • 3
    @jlliagre Eye of the beholder. Aug 7, 2015 at 7:07
  • 2
    @macmadness86 alternate screenshot added
    – jlliagre
    Aug 7, 2015 at 7:20
  • 1
    fyi, htop comes with other color shemes which you can access by going to setup F2. Nov 28, 2016 at 20:09
  • I run with alias top='htop --no-color' so I can type 'top' and get a reasonable replacement for top without the horrible color scheme. Jul 12, 2019 at 18:18
41

top -M doesn't work on any of the Fedora, Debian or Ubuntu distros to my knowledge. I just tried it and it's not in the procps-ng package that provides top. There are many implementations of top so one needs to pay special attention to which they use.

In general it's best to use free with switching to get the amount of memory free on Linux.

procps vs. procps-ng

You might have noticed that on CentOS 5 & 6 as well as RHEL 5 & 6 that top -M appears to work. This is because those distros ship with the original version of procps. The project was forked and there is now another project procps-ng.

Some of the details as to why there was a fork, from the Fedora Project's page.

excerpt

Old (legacy) procps tools had no updates for several years and that led to a massive code split caused by a local-only application of distribution specific patches, which were not merged upstream. The project became hardly maintainable since some of the newly written patches were incompatible with sources maintained by other distributors. A similar incompatibility could be noticed in the applications behavior and their command line switches. This inevitable update can be understood as an effort to unify the procps tools across all Linux distributions.

So to be clear, the forked project, procps-ng is what Debian, Fedora, Ubuntu, and other distros are using, the legacy project, which does support top -M is still in use of several of the longer term releases that don't keep up with the latest and greatest.

NOTE: I downloaded the latest version of procps-ng, "procps-ng version 3.3.9.1-14ef" and it too was lacking the -M switch.

$ ./top/top -version
  procps-ng version 3.3.9.1-14ef
Usage:
  lt-top -hv | -bcHiOSs -d secs -n max -u|U user -p pid(s) -o field -w [cols]

free

In running free with switches, you can see the most likely reason as to why the lack of units feature is missing from procps-ng's implementation of top.

$ free -m
             total       used       free     shared    buffers     cached
Mem:          7782       6506       1276          0        504       1726
-/+ buffers/cache:       4274       3507
Swap:         7823       1429       6394
[saml@greeneggs ~]$ free -k
             total       used       free     shared    buffers     cached
Mem:       7969492    6663180    1306312          0     516948    1764780
-/+ buffers/cache:    4381452    3588040
Swap:      8011772    1463456    6548316

Rounding becomes problematic, so I believe, procps's implementation avoids the issue by not offering the ability.

htop

Does an OK job of showing aggregate memory usage.

   ss of htop

atop

In my opinion a better tool for looking at memory.

   ss of atop

nmon

Another useful tool is nmon for looking at system performance.

   ss of nmon

6
  • 1
    top -M works for me in RHEL6 Dec 19, 2013 at 21:58
  • 2
    @AnthonyAnanich - I researched this a bit more. CentOS 5.4's top version "procps version 3.2.8" has the -M switch, CentOS 5.8, also has this switch, "procps version 3.2.7". However Fedora 19 has "procps-ng version 3.3.8" which doesn't support the -M switch.
    – slm
    Dec 19, 2013 at 22:07
  • @AnthonyAnanich - added details to my answer about procps vs. procps-ng.
    – slm
    Dec 19, 2013 at 22:14
  • Great answer, thank you, @slm. I even do not know which of the three answers is the best. Will not award to anyone, I want let other people decide. Jan 22, 2014 at 11:11
  • @AnthonyAnanich - NP. You should consider picking one if you feel that it answers your Q 100%. Accepting an A is important since it signals to the rest of the community that passes by your Q that you as the OP felt this A answered or solved your particular issues. If other A's are outstanding they'll get UV'd as well. Not accepting one, is basically a signal that you as the OP still feel your Q hasn't bee sufficiently answered. The accepted A also gets positioned as the 1st A when ppl see your Q in the future.
    – slm
    Jan 22, 2014 at 12:59
32

You can press the following keys:

  • e -- Change the scaling factor on the summary display
  • Shift+e -- Change the scaling factor on the task
  • Shift+w -- Save current settings
3
  • 1
    e and shift-e are the other way round on my system at least...
    – fifaltra
    Aug 26, 2016 at 9:45
  • 7
    Didn't know about Shift + w , great tip to save my config :)
    – sofly
    Mar 9, 2017 at 18:27
  • This was the only correct, working answer for my case! Dec 9, 2019 at 9:19
6

You can use the command line option E to specify the memory scaling, for example in gigabytes:

$ top -E g

From man top:

-E  :Extend-Memory-Scaling as:  -E  k | m | g | t | p | e
     Instructs top to force summary area memory to be scaled as:
         k - kibibytes
         m - mebibytes
         g - gibibytes
         t - tebibytes
         p - pebibytes
         e - exbibytes

     Later this can be changed with the `E' command toggle.

The -e option can be used with the same options to change the values in the task list.

OS: Ubuntu 20.04 and RHEL 9

1
  • This seems to be the best modern answer. Also of note: you can use W while in top to save your current settings as defaults. So start up with, e.g. top -Eg -eg -o%MEM and press W. Next time you can just run top with settings saved.
    – miken32
    Feb 19 at 16:50
5

So the quick answer : depending on your linux distro, try either :

top -M

OR, after starting top, type capital E (then W to write the config).

One of those should work for nearly everybody (except Solaris, of course, where you'd be lucky to have top at all).

bonus tip : every time you start a top instance on a new install, type ExyzW to save colours and highlighting and units - what a relief!

1
0

On RHEL7 top shift + e or CspsLK ON. You need " E " capital alphabet.

You will get in MiB, Gib, TiB, PiB, EiB. All these you can access.

And also you can you htop command which should be downlaoded and installed on rpm base system.

Thank you. Sagar Dalvi

0

Let's do some magic one liners so that you can alias what you want and never need to remember which letter to hit when in top to get what you want.

The want is to display top like output but having RSS in MBs and I'm adding also a desire to filter out only the program I want.

In the following examples we will use python as the target processes we want to watch. We will, of course, use perl to do that ;)

a. Without headers (if you memorizedps's headers)

  • One time:

    ps auxc | grep python | perl -plae '$F[5]=sprintf q[%0.3fMB],$F[5]/2**10; $_=qq[@F]' | column -t

  • Using watch to emulate top, refreshing every 0.5 sec

    watch -n 0.5 $'ps auxc | grep python | perl -plae \'$F[5]=sprintf q[%0.3fMB],$F[5]/2**10; $_=qq[@F]\' | column -t'

  • Make an alias (bash 4.4 or higher)

    alias watch-python=$'watch -n 0.5 \'ps auxc | grep python | perl -plae "\$F[5]=sprintf q[%0.3fMB],\$F[5]/2**10; \$_=qq[@F]" | column -t | grep python\''

b. With headers (for the rest of us):

  • One time:

    (ps auxc | head -1; ps auxc | grep python | perl -plae '$F[5]=sprintf q[%0.3fMB],$F[5]/2**10; $_=qq[@F]') | column -t

  • Using watch to emulate top, refreshing every 0.5 sec:

    watch -n 0.5 $'(ps auxc | head -1; ps auxc | grep python | perl -plae \'$F[5]=sprintf q[%0.3fMB],$F[5]/2**10; $_=qq[@F]\') | column -t'

  • Make an alias (bash 4.4 or higher)

    alias watch-python=$'watch -n 0.5 \'(ps auxc | head -1; ps auxc | grep python | perl -plae "\$F[5]=sprintf q[%0.3fMB],\$F[5]/2**10; \$_=qq[@F]") | column -t\''

Notes:

  • remove | grep python if you want all processes
  • replace 2**10 with 2**20 and MB with GB if you want GBs instead of MBs
  • replace python with another string that your program starts with. You can inspect the output of ps auxc to see what's the 10th (0-indexed) column says in case you get no output. a python program could be running with the name of the python script and not python itself for example, so make sure to use the name of the script instead.
  • replace %0.3f with %0.2f if you want only 2 decimals for MBs
  • replace watch -n 0.5 with watch -n 3 if you want to refresh every 3 seconds
  • these were tested with bash 5.1.16 - should work with most recent bash versions
  • if you tweak the ps flags in these examples the count of columns may change and the scripts may break.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.