Skip to content

Instantly share code, notes, and snippets.

@ryran
Last active December 19, 2015 15:49
Show Gist options
  • Save ryran/5979561 to your computer and use it in GitHub Desktop.
Save ryran/5979561 to your computer and use it in GitHub Desktop.
Bash function for making it quick & easy to inspect mem-usage of processes.
#!/bin/bash
# Uses pgrep, ps, and awk to sum the RSS & VSZ of any process names or PIDs passed to it
function memsum {
if [[ -z $1 ]]; then
echo "memsum: requires at least one PID or [partial] process name"
return 1
fi
local pid p
until [[ -z $1 ]]; do
if [[ $1 =~ ^[0-9]+$ ]]; then
[[ -d /proc/$1 ]] && pid[$1]=$(</proc/$1/comm)
else
for p in $(pgrep $1); do
pid[$p]=$(</proc/$p/comm)
done
fi
shift
done
[[ -z ${pid[*]} ]] && { echo memsum: no processes or PIDs matched; return 1; }
ps --no-header -orss,vsz ${!pid[*]} |
awk '{ rss+=$1; vsz+=$2 } END { printf "Total RSS: %.1f MiB (%.2f GiB)\nTotal VSZ: %.0f MiB (%.2f GiB)\n", rss/1024, rss/1024/1024, vsz/1024, vsz/1024/1024 }'
printf "\nProcesses matched: "; for p in ${!pid[*]}; do printf " ${pid[p]} ($p)"; done; printf "\n\n"
pidstat -ruh -p $(echo ${!pid[*]} | sed 's/ /,/g') 1 1
}
@ryran
Copy link
Author

ryran commented Jul 11, 2013

Example usage:

[rsaw:~]$ memsum fire
Total RSS: 1003.3 MiB (0.98 GiB)
Total VSZ: 2573 MiB (2.51 GiB)

Processes matched:  firewalld (904) firefox (21581)

Linux 3.9.6-200.fc18.x86_64 (sawzall)   07/11/2013  _x86_64_    (4 CPU)

#      Time       PID    %usr %system  %guest    %CPU   CPU  minflt/s  majflt/s     VSZ    RSS   %MEM  Command
 1373579388       904    0.00    0.00    0.00    0.00     2      0.00      0.00  248368  16820   0.21  firewalld
 1373579388     21581   16.00    3.00    0.00   19.00     0   3191.00      0.00 2399240 1024236  12.71  firefox

And again:

[rsaw:~]$ memsum gnome bird 16339
Total RSS: 740.0 MiB (0.72 GiB)
Total VSZ: 6617 MiB (6.46 GiB)

Processes matched:  gnome-keyring-d (1850) gnome-session (1852) gnome-settings- (2055) gnome-shell-cal (2197) bash (16339) gnome-shell (17150) gnome-pty-helpe (21575) thunderbird (23004)

Linux 3.9.6-200.fc18.x86_64 (sawzall)   07/11/2013  _x86_64_    (4 CPU)

#      Time       PID    %usr %system  %guest    %CPU   CPU  minflt/s  majflt/s     VSZ    RSS   %MEM  Command
 1373579513      1850    0.00    0.00    0.00    0.00     2      0.00      0.00  838828  10916   0.14  gnome-keyring-d
 1373579513      1852    0.00    0.00    0.00    0.00     1      0.00      0.00  574768  11500   0.14  gnome-session
 1373579513      2055    0.00    0.00    0.00    0.00     0      0.00      0.00 1408252  37812   0.47  gnome-settings-
 1373579513      2197    0.00    0.00    0.00    0.00     1      0.00      0.00  440348   8952   0.11  gnome-shell-cal
 1373579513     16339    0.00    0.00    0.00    0.00     0      0.00      0.00  116812   4604   0.06  bash
 1373579513     17150    4.00    1.00    0.00    5.00     2      0.00      0.00 2196260 446408   5.54  gnome-shell
 1373579513     21575    0.00    0.00    0.00    0.00     3      0.00      0.00    8420    688   0.01  gnome-pty-helpe
 1373579513     23004    0.00    0.00    0.00    0.00     0      0.00      0.00 1191880 236892   2.94  thunderbird

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment