I'm looking for somthing like top is to CPU usage. Is there a command line argument for top that does this? Currently, my memory is so full that even 'man top' fails with out of memory :)
11 Answers
From inside top you can try the following:
- Press SHIFT+f
- Press the Letter corresponding to %MEM
- Press ENTER
You might also try:
$ ps -eo pmem,pcpu,vsize,pid,cmd | sort -k 1 -nr | head -5
This will give the top 5 processes by memory usage.
-
23Under Linux, simply press
Mto sort by physical memory usage (RES column). Under *BSD, runtop -o resortop -o size. But htop is a lot nicer and doesn't even consume more memory than top (however it's not part of the basic toolset so you might not have it installed).Gilles 'SO- stop being evil'– Gilles 'SO- stop being evil'2010-12-19 13:38:22 +00:00Commented Dec 19, 2010 at 13:38 -
3I had to press
sto sort by%MEMand, then hit escape for top.Bryce Guinta– Bryce Guinta2016-06-26 20:40:16 +00:00Commented Jun 26, 2016 at 20:40 -
Doesn't work on Solaris 9.saulius2– saulius22020-04-30 14:19:07 +00:00Commented Apr 30, 2020 at 14:19
-
@Steven How can we group process with same parent? Basically firefox shows up in multiple times may be as it spawns multiple child processes. Is it possible to get combined memory usage?Porcupine– Porcupine2021-03-07 21:45:54 +00:00Commented Mar 7, 2021 at 21:45
-
1
$ top>-bash: fork: Cannot allocate memory- In case anyone else appreciates thisCurtis Yallop– Curtis Yallop2021-10-04 22:39:45 +00:00Commented Oct 4, 2021 at 22:39
If you have it installed I like htop once launching it you can press f6, down arrow (to MEM%), enter to sort by memory.
In Solaris the command you would need is:
prstat -a -s size
This will list all processes in order of descending process image size. Note that the latter is based on memory committed to the process by the OS, not its resident physical memory usage.
There are supposedly versions of "top" available for Solaris, but these are not part of the standard installation.
-
Finally something that works on Solaris 9! Thankssaulius2– saulius22020-04-30 14:25:45 +00:00Commented Apr 30, 2020 at 14:25
Once top starts, press F to switch to the sort field screen. Choose one of the fields listed by pressing the key listed on the left; you probably want N for MEM%
-
2If you want MEM%, pressing 'M' does the same stated above. 'c' adds command line parameters to the process list, may be informative for your problem.wag– wag2010-12-19 08:46:22 +00:00Commented Dec 19, 2010 at 8:46
-
Doesn't work on Solaris 9saulius2– saulius22020-04-30 14:21:04 +00:00Commented Apr 30, 2020 at 14:21
This command will identify the top memory consuming processes:
ps -A --sort -rss -o pid,pmem:40,cmd:500 | head -n 6 | tr -s " " ";z"
-
Doesn't work on Solaris 9:
ps: illegal option -- - ps: ort is an invalid non-numeric argument for -s option ps: illegal option -- r ps: s is an invalid non-numeric argument for -s option ps: unknown output format: -o pmem:40 ps: unknown output format: -o cmd:500saulius2– saulius22020-04-30 14:22:03 +00:00Commented Apr 30, 2020 at 14:22
One nice alternative to top is htop. Check it, it is much more user friendly than regular top.
It can be achieved in multiple ways, My favourite one is:
The PS way:
[arif@arif ~]$ ps -eo pid,cmd,%cpu,%mem --sort=-%memWhere,
-e: to select all process-o: to apply to the output formatpid,cmd,%cpu,%mem: Output format in exact order. Here,pcpuandpmemcan be used instead of%cpuand%mem.- But sadly (don't know why) it doesn't work on some machine (Oracle Linux) and some older machine. You can use the following similar alternatives.
[arif@arif ~]$ ps aux --sort '-%mem' --cols 120 | headWhere,
--cols 100: to specify column width of the output as sometimescmdgets very long. This is not necessary if you don't want curtailed commands with arguments.aux: to see every process on the system using BSD syntax
[arif@arif ~]$ ps -eo pid,cmd,%cpu,%mem --sort -rssWhere,
-rss: resident set size, the non-swapped physical memory that a task has used
[arif@arif ~]$ ps aux --sort -rss --cols 120
The top way:
[arif@arif ~]$ top -b -o +%MEM
Where,
-b: to usetopasbatchmode.-o: to override sort fieldname followed by a fieldname%MEM
And you can always use head and/or tail to control the output.
-
Doesn't work on Solaris 9. The
topcmd-lines gives:top: '+%MEM' is not a recognized sorting order. Try one of these: cpu size res timesaulius2– saulius22020-04-30 14:25:07 +00:00Commented Apr 30, 2020 at 14:25
Globally: It's always recommended to use a log analyser tool for logging history logs such as Splunk, ELK etc. So that using query language you can easily get the PIDs and its usage by CPU & memory.
AT SERVER/OS LEVEL: From inside top you can try the following:
Press SHIFT+M ---> This will give you a process which takes more memory in descending order.
You might also try:
$ ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem | head -10
This will give the top 10 processes by memory usage. Also you can use vmstat utility to find the RAM usage at same time not for history.
-
Thanks, the
toptip worked on Solaris 9. Thepscmd-line did not.saulius2– saulius22020-04-30 14:24:08 +00:00Commented Apr 30, 2020 at 14:24
You can try ps aux --sort -rss | head or ps aux | sort -nk +4 | tail
-
Doesn't work on Solaris 9saulius2– saulius22020-04-30 14:22:50 +00:00Commented Apr 30, 2020 at 14:22
This alias works nice for me in a bash shell:
alias mtop='{ echo "Mem% PID Binary"; ps -A --sort -rss -o pid,pmem:40,cmd:500 | tail -n +2 | head -n 10 | tr -s " " | sed -r "s/^\s*([0-9]+) ([0-9.]+) ([a-zA-Z/]+).*$/\2 \1 \3/"; } | column -t -s " "'
This will print out a neat list of processes sorted by memory consumption:
-=# mtop
Mem% PID Binary
22.9 528235 /opt/intellij
5.4 906569 /usr/share/elasticsearch/jdk/bin/java
3.7 544512 /usr/bin/node
2.2 986170 /usr/lib/firefox/firefox
1.9 795138 /usr/bin/node
1.1 795188 /usr/bin/node
1.0 747819 /usr/lib/jvm/java
0.9 599 /usr/bin/pipewire
0.7 414 /usr/lib/Xorg
0.7 544536 /usr/bin/node
On macOS, ps does not support the --sort option.
You can use the BSD-only -m flag to sort by "memory usage," which avoids piping all rows through sort. (The ps man page doesn't specify exactly which "memory usage" metric is used.)
ps -m -aexo pmem,rss,vsize,pid,etime,user,args | head
Or, if you want sorting by CPU: you can use the BSD-only -r flag to sort by cpu usage.
ps -r -aexo pcpu,cputime,pid,etime,user,args | head
-
The question is about memory, not CPU time.Ternvein– Ternvein2023-07-14 09:24:20 +00:00Commented Jul 14, 2023 at 9:24
-
@Ternvein good point, I copied the wrong snippet from my debugging notes ;) Added memory too now.Carl Walsh– Carl Walsh2023-07-20 20:16:08 +00:00Commented Jul 20, 2023 at 20:16