Skip to content

Instantly share code, notes, and snippets.

@junaidpv
Last active August 29, 2015 14:10
Show Gist options
  • Select an option

  • Save junaidpv/3e9c52a7fd4a6102715c to your computer and use it in GitHub Desktop.

Select an option

Save junaidpv/3e9c52a7fd4a6102715c to your computer and use it in GitHub Desktop.
Script to show combined memory usage share by a program along memory size used.
#!/bin/bash
user=$1
ps `if [ -n "$user" ]; then echo "-u $user"; else echo "-A"; fi` --sort -rss -o comm,pmem,rss | awk '
NR == 1 { print; next }
{ a[$1] += $2; b[$1] += $3;}
END {
split("B KB MB GB TB PB", unit)
for (i in a) {
human_readable = size_in_bytes = b[i] * 1024
j = 1
while ((human_readable_temp = int(human_readable / 1024)) != 0) {
human_readable = human_readable_temp
j++
}
printf "%-20s\t%s\t%s%s\t%s\n", i, a[i], human_readable, unit[j], b[i]
}
}
' | awk 'NR>1' | sort -rnk4 | awk '
BEGIN {printf "%-20s\t%%MEM\tSIZE\n", "COMMAND"}
{
printf "%-20s\t%s\t%s\n", $1, $2, $3
}
'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment