Sometimes you will end up with a linux host that is swapping, and many times it will be obvious which process has been chewing up swap (hint: it is always java). But in those cases where it is not as apparent, I whipped up a little bash script to walk the process tree and list each process that is consuming swap space.
#!/bin/bash
SUM=0
OVERALL=0
for DIR in $( find /proc/ -maxdepth 1 -type d -regex "^/proc/[0-9]+" )
do
for SWAP in $( grep Swap $DIR/smaps 2>/dev/null | awk '{ print $2 }' )
do
SUM=$(( $SUM + $SWAP ))
done
if (( $SUM > 0 )); then
PID=$( echo $DIR | cut -d / -f 3 )
PROGNAME=$( ps -p $PID -o comm --no-headers )
echo "PID=$PID swapped $SUM KB ($PROGNAME)"
fi
OVERALL=$(( $OVERALL + $SUM ))
SUM=0
done
echo "Overall swap used: $OVERALL KB"