22

Executing ps ux returns a nice list of process information, easy to grep through or watch. However, there doesn't seem to be much flexibility in the memory usage output; the RSS (resident set size) is printed in kB, which for large processes is hard to read (especially at a glance), and %MEM gives 100 × RSSsystem_memory.

The du utility has a lovely -h flag which prints space in a more user friendly fashion. I have not been able to find anything equivalent for ps. Is there a special formatting trick that can accomplish this?

4 Answers 4

32
ps ux | numfmt --header --to=iec --field 5,6 --padding 6

You need coreutils >= 8.25

I personally prefer this one:

ps -eo pmem,comm,pid,maj_flt,min_flt,rss,vsz --sort -rss | numfmt --header --to=iec --field 4-5 | numfmt --header --from-unit=1024 --to=iec --field 6-7 | column -t | head
0
6

It seems like there is no appropriate flag in ps, so you need to either use a different tool (I personally prefer htop) or mess with ps output a little. I guess you want to stick with ps. Here's a dirty little script I've made as an example:

# get terminal width
WIDTH=`tput cols`
# pipe stdin to awk
cat | \
awk '\
BEGIN {
    # set output format
    CONVFMT="%.2f"
}
NR==1 {
    # search first line for columns that need to be converted from K to M
    for (i=1;i<=NF;i++) 
        # add condition for new columns if you want
        if ($i=="VSZ" || $i=="RSS") {
            # column numbers are stored in an array
            arr[i]=i; 
            $i = $i "(MB)"
        }
}
NR > 1 {
    # edit appropriate columns
    for (i in arr)
        $i=$i/1024; 
}
{
    # print every line
    print $0
}' | \
# format the output into columns and trim it to terminal width
column -t | cut -c 1-$WIDTH

Save it to a file, say prettyps.sh, make it executable:

chmod +x prettyps.sh

and use as follows:

ps ux | /path/to/prettyps.sh

Using this script has the downside of adding extra processes to ps output, but nevertheless it works:

$ ps ux | ./prettyps.sh
USER  PID   %CPU  %MEM  VSZ(MB)  RSS(MB) TTY    STAT  START  TIME   COMMAND
pono  2658  0.0   0.0   358.88   4.29    ?      Sl    02:33  0:00   /usr/bin/gnome-keyring
... output truncated...
pono  4507  0.0   0.0   19.14    1.81    pts/1  S+    03:29  0:00   man                   
pono  4518  0.0   0.0   10.55    0.96    pts/1  S+    03:29  0:00   pager                 
pono  4727  0.7   0.9   1143.59  53.08   ?      Ssl   04:10  0:24   /opt/sublime_text/subl
pono  4742  0.1   0.4   339.05   25.80   ?      Sl    04:10  0:03   /opt/sublime_text/plug
pono  5177  0.0   0.0   19.23    1.32    pts/0  R+    05:05  0:00   ps                    
pono  5178  0.0   0.0   4.34     0.61    pts/0  S+    05:05  0:00   /bin/sh 

Hope this helps to find a way that suits you.

2

Props to @adrianlxt! This is my new alias "pss":

ps -eo pid,pmem,rss,vsz,comm,command --sort -rss | numfmt --header --from-unit=1024 --to=iec --field 3-4 | awk '$3 != 0'

0
0

Yet another solution is to use AWK. For example,

ps hax -o rss,user | awk '{a[$2]+=$1;}END{for(i in a)print i" "int(a[i]/1024+0.5);}'

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .