27

The Linux ps command shows different memory usages like RSS (resident set size), size in kB by default. Is there a way to show in MB or GB, like ls -s --human-readable does?

1
  • 1
    The lack of mention in man ps suggests to me that there is no such built-in option. Jun 21 '17 at 6:24
33

AFAIK you cannot achieve it simply by pure ps command with options. However you can use some text processors like awk and make it to do what you want:

ps afu | awk 'NR>1 {$5=int($5/1024)"M";}{ print;}'

This takes result from ps and then for every line except the first one it replacecs 5th column which is in KB normally, to MB adding M suffix.

You can make it an alias and store it in .bashrc file so you can call it by something like myps.

Most of people are asking how to preserve format or use other units and precision.

For simple version you can use column -t output filter:

ps afu | awk 'NR>1 {$5=int($5/1024)"M";}{ print;}' | column -t

This however does not recognize spaces in last column correctly. Unfortunately we've got to deal with text formatting and prepare our own format string in printf -like format.

ps afu | awk 'NR==1 {o=$0; a=match($0,$11);}; NR>1 {o=$0;$5=int(10*$5/1024)/10"M";}{ printf "%-8s %6s %-5s %-5s %9s %9s %-8s %-4s %-6s %-5s %s\n", $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, substr(o, a);}'

Explanation:

  • NR==1 condition is for first line only (header). We are using original ps output to determine where COMMAND is starting:
    • o=$0 stores unmodified entire line so we can use it later
    • a=match($0,$11) finds location of 11th field (which should be where COMMAND column is starting in original output)
  • NR>1 is for following lines (data). We are changing 5th field:
    • $5=int(10*$5/1024)/10"M" changes value into Megabytes with one decimal place and adds "M" suffix.
    • printf displays all fields in column-like flavor:
      • %-10s means s for string, 10 for 10 characters wide, - for left align
      • %8s means s for string, 8 for 8 characters wide, and because of no - output of this field is right-aligned.
    • substr(o, a) takes substring of original line (hence o stored before) starting from position a calculated in previous condition, so we can have command output displayed with spaces preserved.
5
  • 5
    Modified for GB and with 2 decimal places (e.g. 1.23GB): ps aux | awk '{$5=int(100 * $5/1024/1024)/100"GB";}{ print;}'
    – mahemoff
    Dec 19 '18 at 12:45
  • 2
    is it possible to achieve the same goal preserving the ps formatting? Tabs are completely stripped away with awk
    – Chris
    Aug 22 '19 at 10:24
  • I see you are casting as an int, is it possible to have it decimal like 1.2M ? Aug 30 '19 at 20:20
  • Why tabs are missing?
    – Alex78191
    Apr 3 '20 at 18:22
  • @Alex78191: Added alternate command for formatting with table-like output
    – DevilaN
    Apr 6 '20 at 9:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.