How to display memory used by processes in human readable form

Display memory used by processes in human readable form using ps and awk utilities.

I will use mawk as it is installed by default on Debian based distributions.

$ awk -W version
mawk 1.3.3 Nov 1996, Copyright (C) Michael D. Brennan

compiled limits:
max NF             32767
sprintf buffer      2040

Use ps command to display top twenty processes using the most memory. Notice that Resident Set Size (RSS) is defined in kiloBytes (1024 bytes as this is a memory).

$ ps -ax --no-headers --format=pid,rss,command --sort=-rss,pid | head -20
 5451 648148 /usr/lib/firefox/firefox -contentproc -childID 67 -isForBrowser -prefsLen 7220 -prefMapSize 169730 -schedulerPrefs 0001,2 -parentBuildID 20181023214826 -greomni /usr/lib/firefox/omni.ja -appomni /usr/lib/firefox/browser/omni.ja -appdir /usr/lib/firefox/browser 3941 true tab
 3941 646684 /usr/lib/firefox/firefox -new-window
12932 622172 /usr/lib/firefox/firefox -contentproc -childID 70 -isForBrowser -prefsLen 7220 -prefMapSize 169730 -schedulerPrefs 0001,2 -parentBuildID 20181023214826 -greomni /usr/lib/firefox/omni.ja -appomni /usr/lib/firefox/browser/omni.ja -appdir /usr/lib/firefox/browser 3941 true tab
 8580 549512 /usr/lib/firefox/firefox -contentproc -childID 10 -isForBrowser -prefsLen 6427 -prefMapSize 169730 -schedulerPrefs 0001,2 -parentBuildID 20181023214826 -greomni /usr/lib/firefox/omni.ja -appomni /usr/lib/firefox/browser/omni.ja -appdir /usr/lib/firefox/browser 3941 true tab
14041 536904 /usr/lib/virtualbox/VirtualBox --comment Debian --startvm dabea2f0-5c6e-4d44-9f37-8dfc070e1ff8 --no-startvm-errormsgbox
17219 487292 /usr/lib/firefox/firefox -contentproc -childID 29 -isForBrowser -prefsLen 7218 -prefMapSize 169730 -schedulerPrefs 0001,2 -parentBuildID 20181023214826 -greomni /usr/lib/firefox/omni.ja -appomni /usr/lib/firefox/browser/omni.ja -appdir /usr/lib/firefox/browser 3941 true tab
 3477 406112 /usr/bin/gnome-shell
 4272 268492 /usr/bin/ruby /usr/bin/jekyll server --future -w
  467 128224 /lib/systemd/systemd-journald
 4088 112420 /usr/lib/firefox/firefox -contentproc -childID 2 -isForBrowser -prefsLen 226 -prefMapSize 169730 -schedulerPrefs 0001,2 -parentBuildID 20181023214826 -greomni /usr/lib/firefox/omni.ja -appomni /usr/lib/firefox/browser/omni.ja -appdir /usr/lib/firefox/browser 3941 true tab
 3287 99784 /usr/lib/xorg/Xorg vt2 -displayfd 3 -auth /run/user/1000/gdm/Xauthority -background none -noreset -keeptty -verbose 3
 1825 78204 /usr/bin/gnome-shell
  309 76452 keepassxc
13972 68024 /usr/lib/virtualbox/VirtualBox
 4063 62096 /usr/sbin/netdata -P /run/netdata/netdata.pid -D
 4161 58964 /usr/bin/python /usr/libexec/netdata/plugins.d/python.d.plugin 1
 3830 56316 /usr/lib/gnome-terminal/gnome-terminal-server
 4416 32520 /usr/sbin/libvirtd
 4162 27772 /usr/bin/python3 /usr/bin/chrome-gnome-shell /usr/lib/mozilla/native-messaging-hosts/org.gnome.chrome_gnome_shell.json [email protected]
18463 27396 /usr/lib/packagekit/packagekitd

Solution to this issue is to use awk to parse and pretty print Resident Set Size (RSS).

$ ps -ax --no-headers --format=pid,rss,command --sort=-rss,pid | head -20 | awk 'BEGIN{suffixes_len=split("kB MB GB TB",suffixes)} {n_suffix=1; while($2 > 1000 && n_suffix < suffixes_len) {$2 /= 1024; n_suffix++;}; printf "%5s %7.2f %2s %s\n",$1,$2,suffixes[n_suffix],substr($0, index($0,$3))}'
 5451  632.96 MB /usr/lib/firefox/firefox -contentproc -childID 67 -isForBrowser -prefsLen 7220 -prefMapSize 169730 -schedulerPrefs 0001,2 -parentBuildID 20181023214826 -greomni /usr/lib/firefox/omni.ja -appomni /usr/lib/firefox/browser/omni.ja -appdir /usr/lib/firefox/browser 3941 true tab
 3941  631.53 MB /usr/lib/firefox/firefox -new-window
12932  607.59 MB /usr/lib/firefox/firefox -contentproc -childID 70 -isForBrowser -prefsLen 7220 -prefMapSize 169730 -schedulerPrefs 0001,2 -parentBuildID 20181023214826 -greomni /usr/lib/firefox/omni.ja -appomni /usr/lib/firefox/browser/omni.ja -appdir /usr/lib/firefox/browser 3941 true tab
 8580  536.63 MB /usr/lib/firefox/firefox -contentproc -childID 10 -isForBrowser -prefsLen 6427 -prefMapSize 169730 -schedulerPrefs 0001,2 -parentBuildID 20181023214826 -greomni /usr/lib/firefox/omni.ja -appomni /usr/lib/firefox/browser/omni.ja -appdir /usr/lib/firefox/browser 3941 true tab
14041  524.32 MB /usr/lib/virtualbox/VirtualBox --comment Debian --startvm dabea2f0-5c6e-4d44-9f37-8dfc070e1ff8 --no-startvm-errormsgbox
17219  475.87 MB /usr/lib/firefox/firefox -contentproc -childID 29 -isForBrowser -prefsLen 7218 -prefMapSize 169730 -schedulerPrefs 0001,2 -parentBuildID 20181023214826 -greomni /usr/lib/firefox/omni.ja -appomni /usr/lib/firefox/browser/omni.ja -appdir /usr/lib/firefox/browser 3941 true tab
 3477  396.59 MB /usr/bin/gnome-shell
 4272  262.20 MB /usr/bin/ruby /usr/bin/jekyll server --future -w
  467  125.22 MB /lib/systemd/systemd-journald
 4088  109.79 MB /usr/lib/firefox/firefox -contentproc -childID 2 -isForBrowser -prefsLen 226 -prefMapSize 169730 -schedulerPrefs 0001,2 -parentBuildID 20181023214826 -greomni /usr/lib/firefox/omni.ja -appomni /usr/lib/firefox/browser/omni.ja -appdir /usr/lib/firefox/browser 3941 true tab
 3287   97.45 MB /usr/lib/xorg/Xorg vt2 -displayfd 3 -auth /run/user/1000/gdm/Xauthority -background none -noreset -keeptty -verbose 3
 1825   76.37 MB /usr/bin/gnome-shell
  309   74.66 MB keepassxc
13972   66.43 MB /usr/lib/virtualbox/VirtualBox
 4063   60.64 MB /usr/sbin/netdata -P /run/netdata/netdata.pid -D
 4161   57.58 MB /usr/bin/python /usr/libexec/netdata/plugins.d/python.d.plugin 1
 3830   55.00 MB /usr/lib/gnome-terminal/gnome-terminal-server
 4416   31.76 MB /usr/sbin/libvirtd
 4162   27.12 MB /usr/bin/python3 /usr/bin/chrome-gnome-shell /usr/lib/mozilla/native-messaging-hosts/org.gnome.chrome_gnome_shell.json [email protected]
18463   26.75 MB /usr/lib/packagekit/packagekitd
Milosz Galazka's Picture

About Milosz Galazka

Milosz is a Linux Foundation Certified Engineer working for a successful Polish company as a system administrator and a long time supporter of Free Software Foundation and Debian operating system.