Find what program is using swap

Submitted by sklav on Mon, 05/14/2012 - 11:43

Here is a nice little script i found online that shows what process is using swap. I have done a minor tweak to remove processes that return a value of zero.

#!/bin/bash
# Get current swap usage for all running processes
# Erik Ljungstrom 27/05/2011 and tweaked output to remove zero values by Nick Sklavenitis on May 14th 2012
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
echo "PID=$PID - Swap used: $SUM - ($PROGNAME )" |grep -v "Swap used: 0"|sort -n -k 5
let OVERALL=$OVERALL+$SUM
SUM=0

done
echo "Overall swap used: $OVERALL"


The following sript was found on the following website northernmost.org I just added it here with a minor tweak cause it is easier for me to find it when i need it.