Finding out per process Swap usage

In this post, I will share with you useful shell script to find per process swap usage on your system.

Method- 1

total_swap=0
for pid in `ls /proc | grep "[[:digit:]]"| sort -n`
do
    if [[ "$pid" -gt "0" && -f "/proc/$pid/status" ]]; then
        pname=`cat /proc/$pid/status|awk -F"Name:" '{print $2}'`
        swap_usage=`cat /proc/$pid/smaps | awk 'BEGIN{total=0}/^Swap:/{total+=$2}END{print total}'`
        echo "$pname($pid): $swap_usage KB"
        total_swap=$((total_swap + swap_usage))
    fi
done
echo "Total Swap: $total_swap Kb"

Method-2

cat /proc/{pid}/status|awk '/VmSwap:/{print $2 " " $3}'

Finding Out Memory Usage without adding shared memory (non-shared):

smem utility.

Unshared memory is reported as the USS (Unique Set Size).

The unshared memory (USS) plus a process’s proportion of shared memory is reported as the PSS (Proportional Set Size).”

The USS and PSS only include physical memory usage. They do not include memory that has been swapped out to disk.”

#add epel-release repo.
[root@rhce ~]# yum install smem
[root@rhce ~]#  smem -u ansible
User     Count     Swap      USS      PSS      RSS
nobody       1      184        0        2       92
colord       1     1544        4        5      192
libstoragemgmt     1      116       40       40       60
rtkit        1      184       48       48      140
avahi        2      448      152      170      448
postfix      2     1900      168      171      420
haproxy      2     2732      264      305      560
dbus         1      432     1980     2010     2364
polkitd      1    10904     2780     2897     3856
root        53   146564    97156   101862   119260
ansible     64   413824   477768   481342   501392

Finding out memory statistics as percentage.

[root@rhce ~]# smem -w -p
Area                           Used      Cache   Noncache
firmware/hardware             0.00%      0.00%      0.00%
kernel image                  0.00%      0.00%      0.00%
kernel dynamic memory        30.13%     10.81%     19.31%
userspace memory             58.54%      3.20%     55.33%
free memory                  11.34%     11.34%      0.00%

Show totals and percentage:

 PID User     Command                         Swap      USS      PSS      RSS
77794 ansible  /usr/libexec/gnome-terminal    0.84%    0.23%    0.23%    0.26%
72241 root     /usr/sbin/rsyslogd -n          1.13%    0.26%    0.28%    0.33%
  678 polkitd  /usr/lib/polkit-1/polkitd -    0.69%    0.29%    0.30%    0.40%
 5688 ansible  /usr/libexec/gnome-settings    0.42%    0.32%    0.33%    0.44%
    1 root     /usr/lib/systemd/systemd --    0.07%    0.36%    0.37%    0.41%
 5919 ansible  /usr/libexec/tracker-store     0.50%    0.45%    0.45%    0.47%
 5827 ansible  /usr/libexec/caribou           0.16%    0.65%    0.65%    0.68%
78786 ansible  evince                         0.00%    0.78%    0.80%    0.85%
78818 ansible  eog                            0.00%    0.83%    0.83%    0.85%
 5900 ansible  nautilus --no-default-windo    0.27%    0.89%    0.92%    0.98%
81293 root     python /bin/smem -t -p         0.00%    1.03%    1.07%    1.16%
78686 ansible  /usr/lib64/libreoffice/prog    4.10%    3.40%    3.41%    3.45%
78843 ansible  /usr/lib/jvm/java-1.8.0-ope    0.00%    4.97%    5.03%    5.12%
 1452 root     /usr/bin/Xorg :0 -backgroun    2.21%    5.93%    6.13%    6.34%
78519 ansible  /usr/lib64/firefox/firefox     2.02%   10.02%   10.03%   10.12%
 5733 ansible  /usr/bin/gnome-shell           8.21%   24.20%   24.25%   24.41%
-------------------------------------------------------------------------------
  130 11                                     36.78%   58.20%   59.07%   63.11%

Show as  a Chart:

To show as  a chart you have to install matpilotlib package

[root@rhce ~]# yum install python-matplotlib.x86_64

Plotting specific process: 

smem --userfilter="haproxy" --bar name -c"pss rss"
[root@rhce ~]# pidof haproxy
76708 76706

 

 

 

 

 

 

 

 

 

 

 

Plotting Swap usage:

smem  --processfilter="gnome-shell" --bar name -c "pss uss swap"

 

 

 

 

 

 

 

 

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.