John Reed
I found a one-liner to output the per-process memory usage in human-readable format; however, it included an errant line at the beginning and also included processes with 0 memory usage. By modifying the awk command I was able to suppress the display of processes with no memory usage.
Here is a one-liner that only lists processes with non-zero memory usage:
ps -eo size,command --sort -size | awk '$1~/^[1-9]/{printf("%13.2f MB ",$1/1024);for (x=2;x<=NF;x++){printf("%s ",$x)};print ""}'
If some of the lines are very long and keep wrapping onto the next line (or even subsequent lines), you can limit the maximum number of characters output for each process command by using a modified version of the one-liner below. It’s current set to limit the command to 62 characters (enough for a standard 80 character wide terminal window).
ps -eo size,command --sort -size | awk '$1~/^[1-9]/{printf("%13.2f MB ",$1/1024);var="";for (x=2;x<=NF;x++){var=var" "$x};printf("%.62s",var);print ""}'
Example
john@ubuntu:~$ ps -eo size,command --sort -size | awk '$1~/^[1-9]/{printf("%13.2f MB ",$1/1024);for (x=2;x<=NF;x++){printf("%s ",$x)};print ""}'
921.89 MB /usr/sbin/apache2 -k start
352.40 MB ./Plex Media Server
216.80 MB whoopsie
216.47 MB rsyslogd
References
- http://php.ingewikkeld.net/entry/human-readable-memory-usage-in-linux-per-process
- https://www.digitalocean.com/community/tutorials/how-to-use-the-awk-language-to-manipulate-text-in-linux