[linux] How to see top processes sorted by actual memory usage?

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.

This question is related to linux memory ram opensuse

The answer is


List and Sort Processes by Memory Usage:

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

How to total up used memory by process name:

Sometimes even looking at the biggest single processes there is still a lot of used memory unaccounted for. To check if there are a lot of the same smaller processes using the memory you can use a command like the following which uses awk to sum up the total memory used by processes of the same name:

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

e.g. output

9344 docker 1
9948 nginx: 4
22500 /usr/sbin/NetworkManager 1
24704 sleep 69
26436 /usr/sbin/sshd 15
34828 -bash 19
39268 sshd: 10
58384 /bin/su 28
59876 /bin/ksh 29
73408 /usr/bin/python 2
78176 /usr/bin/dockerd 1
134396 /bin/sh 84
5407132 bin/naughty_small_proc 1432
28061916 /usr/local/jdk/bin/java 7

you can specify which column to sort by, with following steps:

steps:
* top
* shift + F
* select a column from the list
    e.g. n means sort by memory,
* press enter
* ok

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 :-)


This very second in time

ps -U $(whoami) -eom pid,pmem,pcpu,comm | head -n4

Continuously updating

watch -n 1 'ps -U $(whoami) -eom pid,pmem,pcpu,comm | head -n4'

I also added a few goodies here you might appreciate (or you might ignore)

-n 1 watch and update every second

-U $(whoami) To show only your processes. $(some command) evaluates now

| head -n4 To only show the header and 3 processes at a time bc often you just need high usage line items

${1-4} says my first argument $1 I want to default to 4, unless I provide it

If you are using a mac you may need to install watch first brew install watch

Alternatively you might use a function

psm(){
    watch -n 1 "ps -eom pid,pmem,pcpu,comm | head -n ${1-4}"
    # EXAMPLES: 
    # psm 
    # psm 10
}

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


You have this simple command:

$ free -h

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

(Adding -n numeric flag to sort command.)


You can see memory usage by executing this code in your terminal:

$ watch -n2 free -m
$ htop

Building on gaoithe's answer, I attempted to make the memory units display in megabytes, and sorted by memory descending limited to 15 entries:

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 }'

       588.03M  /usr/sbin/apache2
       275.64M  /usr/sbin/mysqld
       138.23M  vim
        97.04M  -bash
        40.96M  ssh
        34.28M  tmux
        17.48M  /opt/digitalocean/bin/do-agent
        13.42M  /lib/systemd/systemd-journald
        10.68M  /lib/systemd/systemd
        10.62M  /usr/bin/redis-server
         8.75M  awk
         7.89M  sshd:
         4.63M  /usr/sbin/sshd
         4.56M  /lib/systemd/systemd-logind
         4.01M  /usr/sbin/rsyslogd

Here's an example alias to use it in a bash config file:

    alias topmem="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 }'"

Then you can just type topmem on the command line.


ps aux --sort '%mem'

from procps' ps (default on Ubuntu 12.04) generates output like:

USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
...
tomcat7   3658  0.1  3.3 1782792 124692 ?      Sl   10:12   0:25 /usr/lib/jvm/java-7-oracle/bin/java -Djava.util.logging.config.file=/var/lib/tomcat7/conf/logging.properties -D
root      1284  1.5  3.7 452692 142796 tty7    Ssl+ 10:11   3:19 /usr/bin/X -core :0 -seat seat0 -auth /var/run/lightdm/root/:0 -nolisten tcp vt7 -novtswitch
ciro      2286  0.3  3.8 1316000 143312 ?      Sl   10:11   0:49 compiz
ciro      5150  0.0  4.4 660620 168488 pts/0   Sl+  11:01   0:08 unicorn_rails worker[1] -p 3000 -E development -c config/unicorn.rb             
ciro      5147  0.0  4.5 660556 170920 pts/0   Sl+  11:01   0:08 unicorn_rails worker[0] -p 3000 -E development -c config/unicorn.rb             
ciro      5142  0.1  6.3 2581944 239408 pts/0  Sl+  11:01   0:17 sidekiq 2.17.8 gitlab [0 of 25 busy]                                                                          
ciro      2386  3.6 16.0 1752740 605372 ?      Sl   10:11   7:38 /usr/lib/firefox/firefox

So here Firefox is the top consumer with 16% of my memory.

You may also be interested in:

ps aux --sort '%cpu'

Examples related to linux

grep's at sign caught as whitespace How to prevent Google Colab from disconnecting? "E: Unable to locate package python-pip" on Ubuntu 18.04 How to upgrade Python version to 3.7? Install Qt on Ubuntu Get first line of a shell command's output Cannot connect to the Docker daemon at unix:/var/run/docker.sock. Is the docker daemon running? Run bash command on jenkins pipeline How to uninstall an older PHP version from centOS7 How to update-alternatives to Python 3 without breaking apt?

Examples related to memory

How does the "view" method work in PyTorch? How do I release memory used by a pandas dataframe? How to solve the memory error in Python Docker error : no space left on device Default Xmxsize in Java 8 (max heap size) How to set Apache Spark Executor memory What is the best way to add a value to an array in state How do I read a large csv file with pandas? How to clear variables in ipython? Error occurred during initialization of VM Could not reserve enough space for object heap Could not create the Java virtual machine

Examples related to ram

How to delete multiple pandas (python) dataframes from memory to save RAM? How can I find out the total physical memory (RAM) of my linux box suitable to be parsed by a shell script? What are the differences between virtual memory and physical memory? Sorting 1 million 8-decimal-digit numbers with 1 MB of RAM how much memory can be accessed by a 32 bit machine? How to see top processes sorted by actual memory usage? How much RAM is SQL Server actually using? MySQL maximum memory usage How to get current CPU and RAM usage in Python? How do I check CPU and Memory Usage in Java?

Examples related to opensuse

How to see top processes sorted by actual memory usage?