What process is using how much swap memory

I found this interesting script that prints out a list of processes that are using swap space. I changed the output to suppress the PIDs that aren’t using swap.

#!/bin/bash
# Get current swap usage for all running processes
# Erik Ljungstrom 27/05/2011
# Updated: 2013-11-13 Yuichiro Saito
# Updated: 2014-08-04 Juergen Caris
SUM=0
OVERALL=0
for DIR in `find /proc/ -maxdepth 1 -type d | egrep "^/proc/[0-9]"` ; do
        PID=`echo $DIR | cut -d / -f 3`
        PROGNAME=`ps -p $PID -o comm --no-headers`
        for SWAP in `grep Swap $DIR/smaps 2>/dev/null| awk '{ print $2 }'`
        do
                let SUM=$SUM+$SWAP
        done
        if [ $SUM -gt 0 ]
        then
                echo "PID=$PID - Swap used: $SUM - (${PROGNAME})"
                let OVERALL=$OVERALL+$SUM
                SUM=0
        fi
done
echo "Overall swap used: $OVERALL"

About Juergen Caris

I am 54yo, MSc(Dist) and BSc in Computer Science, German and working as a Senior Server Engineer for the NHS Lothian. I am responsible for the patient management system, called TrakCare. I am a UNIX/Linux guy, working in this sector for more than 20 years now. I am also interested in robotics, microprocessors, system monitoring, Home automation and programming.
This entry was posted in Bash, Hardware, Linux, Uncategorized. Bookmark the permalink.

Leave a Reply

Your email address will not be published.