How to see top processes sorted by actual memory usage?

538,915

Solution 1

First, repeat this mantra for a little while: "unused memory is wasted memory". The Linux kernel keeps around huge amounts of file metadata and files that were requested, until something that looks more important pushes that data out. It's why you can run:

find /home -type f -name '*.mp3'
find /home -type f -name '*.aac'

and have the second find instance run at ridiculous speed.

Linux only leaves a little bit of memory 'free' to handle spikes in memory usage without too much effort.

Second, you want to find the processes that are eating all your memory; in top use the M command to sort by memory use. Feel free to ignore the VIRT column, that just tells you how much virtual memory has been allocated, not how much memory the process is using. RES reports how much memory is resident, or currently in ram (as opposed to swapped to disk or never actually allocated in the first place, despite being requested).

But, since RES will count e.g. /lib/libc.so.6 memory once for nearly every process, it isn't exactly an awesome measure of how much memory a process is using. The SHR column reports how much memory is shared with other processes, but there is no guarantee that another process is actually sharing -- it could be sharable, just no one else wants to share.

The smem tool is designed to help users better gage just how much memory should really be blamed on each individual process. It does some clever work to figure out what is really unique, what is shared, and proportionally tallies the shared memory to the processes sharing it. smem may help you understand where your memory is going better than top will, but top is an excellent first tool.

Solution 2

use quick tip using top command in linux/unix

$ top

and then hit Shift+m (i.e. write a capital M).

From man top

SORTING of task window
  For compatibility, this top supports most of the former top sort keys.
  Since this is primarily a service to former top users, these commands do
  not appear on any help screen.
    command   sorted-field                  supported
      A         start time (non-display)      No
      M         %MEM                          Yes
      N         PID                           Yes
      P         %CPU                          Yes
      T         TIME+                         Yes

Or alternatively: hit Shift + f , then choose the display to order by memory usage by hitting key n then press Enter. You will see active process ordered by memory usage

Solution 3

ps aux | awk '{print $2, $4, $11}' | sort -k2rn | head -n 10

(Adding -n numeric flag to sort command.)

Solution 4

First you should read an explanation on the output of free. Bottom line: you have at least 10.7 GB of memory readily usable by processes.

Then you should define what "memory usage" is for a process (it's not easy or unambiguous, trust me).

Then we might be able to help more :-)

Solution 5

List and Sort Processes by Memory Usage:

ps -e -orss=,args= | sort -b -k1,1n | pr -TW$COLUMNS
Share:
538,915

Related videos on Youtube

user3111525
Author by

user3111525

Updated on August 29, 2020

Comments

  • user3111525
    user3111525 over 3 years

    I have a server with 12G of memory. A fragment of top is shown below:

    PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND                                                                                                                                                                                                                                                      
    12979 frank  20   0  206m  21m  12m S   11  0.2  26667:24 krfb                                                                                                                                                                                                                                                          
    13 root      15  -5     0    0    0 S    1  0.0  36:25.04 ksoftirqd/3                                                                                                                                                                                                                                                   
    59 root      15  -5     0    0    0 S    0  0.0   4:53.00 ata/2                                                                                                                                                                                                                                                         
    2155 root      20   0  662m  37m 8364 S    0  0.3 338:10.25 Xorg                                                                                                                                                                                                                                                          
    4560 frank  20   0  8672 1300  852 R    0  0.0   0:00.03 top                                                                                                                                                                                                                                                           
    12981 frank  20   0  987m  27m  15m S    0  0.2  45:10.82 amarok                                                                                                                                                                                                                                                        
    24908 frank  20   0 16648  708  548 S    0  0.0   2:08.84 wrapper                                                                                                                                                                                                                                                       
    1 root      20   0  8072  608  572 S    0  0.0   0:47.36 init                                                                                                                                                                                                                                                          
    2 root      15  -5     0    0    0 S    0  0.0   0:00.00 kthreadd
    

    The free -m shows the following:

                 total       used       free     shared    buffers     cached
    Mem:         12038      11676        362          0        599       9745
    -/+ buffers/cache:       1331      10706
    Swap:         2204        257       1946
    

    If I understand correctly, the system has only 362 MB of available memory. My question is: How can I find out which process is consuming most of the memory?

    Just as background info, the system is running 64bit OpenSuse 12.

  • user3111525
    user3111525 over 13 years
    Where did you get 10.7 from? From buffers/cache [free]? Thanks for the link, I will read it.
  • stolsvik
    stolsvik about 11 years
    Yes. The point is that most of the memory is used by buffers and cache. This memory can be "dumped" right away if any process needs more memory. When you subtract the amount of memory used for buffers/cache from the USED amount, or add it to FREE amount, you get the numbers on the second line, which then imples that only 1.3 gig is really used, or, seen from the other angle, you have 10.7 gig readily available memory (since buffers and cache can be insta-dumped on demand).
  • Patryk
    Patryk almost 11 years
    Or you can just press M ( Shift+ m )
  • glerYbo
    glerYbo over 10 years
    Or: ps -e -orss=,args= | sort -nr | head
  • codecowboy
    codecowboy over 10 years
    @risnandar is there a way to show memory in MB and not %
  • risnandar
    risnandar over 10 years
    hi codecowboy, perhaps you can look at commandlinefu.com/commands/view/3/… for more detailed memory used in my server i am using third party app like newrelic.com
  • Ciro Santilli OurBigBook.com
    Ciro Santilli OurBigBook.com about 9 years
    Also consider ps aux --sort '%mem' as explained in my answer
  • benjaoming
    benjaoming about 9 years
    Duplicate of risnandar's answer above.
  • skipy
    skipy almost 9 years
    or just top -o mem -O cpu
  • Al2O3
    Al2O3 almost 8 years
    So if only "free" column statistic drops, top shows nothing else different, we can conclude the memory is allocated by linux kernel to store file things and will give the memory to other processes when nessary?
  • sarnold
    sarnold almost 8 years
    @Rubby, that's probably true; the /proc/meminfo and /proc/slabinfo files detail what the kernel is using the storage for -- the slabtop program is very much like top, but shows which of the slab allocators have allocated how much, what their ratios are like, etc.
  • JosephK
    JosephK almost 8 years
    Thanks for the tip on 'smem' - I want Linux to "waste" some RAM so my machine can run fast. If 'find' takes a little longer on a 2nd pass, it's ok. A stuck-mouse and frozen windows while Linux decides which RAM (which it has unnecessarily hogged), needs to be cleared and re-allocated to what I am doing NOW - or even swaps to disk - is not an option. I have 16 GB of ram on this box, and I expect several GB of that to be kept free and available to running applications.
  • sarnold
    sarnold almost 8 years
    @JosephK, moving a mouse has more to do with scheduling priorities and algorithms; if memory allocations are necessary to move a mouse pointer then something is seriously wrong with the software you're using. :)
  • JosephK
    JosephK almost 8 years
    @sarnold - Those are just the symptoms of the problem. I first looked for CPU hogs, but found very low cpu usage while experiencing those symptoms. After I found the memory-hogs and killed those processes, I had some gigs of ram free - and the symptoms went away.
  • AdamS
    AdamS about 7 years
    On Ubuntu 16 I needed top -o RES otherwise "unrecognised field name 'mem'"
  • edwardsmarkf
    edwardsmarkf about 6 years
    "unused memory is wasted memory" - love it!- sounds like the perfect tattoo.
  • David Schwartz
    David Schwartz over 5 years
    @JosephK It actually takes the kernel less time to repurpose memory from one use to another than to put free memory into use. One requires acceessing and modifying the free list, the other doesn't. Unfortunately, this is an XY question. The problem has to do with performance and may be entirely unrelated to memory consumption (despite the evidence that making more memory free helps it, that may be for more complex reasons than the OP suspects) but instead they asked about analyzing memory usage. That gets less useful answers than asking about the actual problem.
  • jasonleonhard
    jasonleonhard almost 4 years
    And you can easily sort by cpu if you change -m to -r
  • arif
    arif almost 4 years
    shouldn't it be -%mem and -%cpu ?
  • Ciro Santilli OurBigBook.com
    Ciro Santilli OurBigBook.com almost 4 years
    @muhammad the advantage of not having - is that the largest shows last on my terminal, so I don't have to less it.
  • arif
    arif almost 4 years
    Cool! I usually head it. So I was a bit confused. Thanks.
  • kjones
    kjones over 3 years
    Nice, any way to show memory in different units (like the -h, --human-readable flag to ls)?
  • kjones
    kjones over 3 years
    I reversed the sort, limited to 15 entries, and attempted to calculate megabytes: ps -e -orss=,args= |awk '{print $1 " " $2 }'| awk '{tot[$2]+=$1;count[$2]++} END {for (i in tot) {print tot[i],i,count[i]}}' | sort -n | tail -n 15 | sort -nr | awk '{ hr=$1/1024; printf("%13.2fM", hr); print "\t" $2 }'
  • gaoithe
    gaoithe over 3 years
    ps doesn't have a -h human-readable option and there doesn't seem to be a standard tool for conversion, numfmt seems a bit limited, awk and numfmt info here: unix.stackexchange.com/questions/44040/…
  • nyxee
    nyxee about 3 years
    How do I edit this to chop the output of command. So that the command string can appear nicely on the line when the command was too long. ps -eo rss,pid,user,command | sort -rn | head -$1 | awk { hr[1024**2]="GB"; hr[1024]="MB"; for (x=1024**3; x>=1024; x/=1024) { if ($1>=x) { printf ("%-6.2f %s ", $1/x, hr[x]); break } } } { printf ("%-6s %-10s ", $2, $3) } { for ( x=4 ; x<=NF ; x++ ) { printf ("%s ",$x) } print ("\n") } '