How to see top processes sorted by actual memory usage?

前端 未结 12 979
栀梦
栀梦 2020-12-07 06:53

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                              


        
12条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-07 07:22

    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.

提交回复
热议问题