How to see top processes sorted by actual memory usage?
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
Related videos on Youtube
user3111525
Updated on August 29, 2020Comments
-
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
.-
Vijay about 10 years
-
-
user3111525 over 13 yearsWhere did you get 10.7 from? From buffers/cache [free]? Thanks for the link, I will read it.
-
stolsvik about 11 yearsYes. 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 almost 11 yearsOr you can just press
M
(Shift
+m
) -
glerYbo over 10 yearsOr:
ps -e -orss=,args= | sort -nr | head
-
codecowboy over 10 years@risnandar is there a way to show memory in MB and not %
-
risnandar over 10 yearshi 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 about 9 yearsAlso consider
ps aux --sort '%mem'
as explained in my answer -
benjaoming about 9 yearsDuplicate of risnandar's answer above.
-
skipy almost 9 yearsor just
top -o mem -O cpu
-
Al2O3 almost 8 yearsSo 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 almost 8 years@Rubby, that's probably true; the
/proc/meminfo
and/proc/slabinfo
files detail what the kernel is using the storage for -- theslabtop
program is very much liketop
, but shows which of the slab allocators have allocated how much, what their ratios are like, etc. -
JosephK almost 8 yearsThanks 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 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 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 about 7 yearsOn Ubuntu 16 I needed
top -o RES
otherwise "unrecognised field name 'mem'" -
edwardsmarkf about 6 years"unused memory is wasted memory" - love it!- sounds like the perfect tattoo.
-
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 almost 4 yearsAnd you can easily sort by cpu if you change
-m
to-r
-
arif almost 4 yearsshouldn't it be
-%mem
and-%cpu
? -
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 toless
it. -
arif almost 4 yearsCool! I usually
head
it. So I was a bit confused. Thanks. -
kjones over 3 yearsNice, any way to show memory in different units (like the
-h, --human-readable
flag tols
)? -
kjones over 3 yearsI 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 over 3 yearsps 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 about 3 yearsHow 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") } '