Find out which processes are swapping

Have you ever been complained that servers are getting slower and slower, and with the command free, you see that a bit of swap is used and wondering what processes are swapping ?

It’s usually not very indicative of anything, or even overly helpful knowing what’s there, mostly just a curiosity thing.

Anyway, starting from kernel 2.6.14, we can find out how much swap are being used by each running process. These details can be found in /proc. Below is a simple bash script which prints out running processes with their swap usage if they do use swap space. More details about smaps can be found here.

# Get current swap usage for running processes
# Erik Ljungstrom <> 27/05/2011


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}'`
          let SUM=$SUM+$SWAP

     [ $SUM -gt 0 ] && echo "PID=$PID - Swap used: $SUM KBytes- ($PROGNAME)"

echo "Overall swap used: $OVERALL KBytes"

As it checks all running processes, root permission is needed to run this script. It still works even if you don’t run with root permission, and it report 0 for any processes not owned by current user. Below is an output example:

$ sudo /tmp/
PID=2817 - Swap used: 116 KBytes- (brcm_iscsiuio)
PID=2828 - Swap used: 76 KBytes- (iscsid)
PID=3011 - Swap used: 184 KBytes- (auditd)
PID=3013 - Swap used: 148 KBytes- (audispd)
PID=3035 - Swap used: 100 KBytes- (syslogd)
PID=3038 - Swap used: 84 KBytes- (klogd)
PID=3048 - Swap used: 56 KBytes- (irqbalance)
PID=3066 - Swap used: 128 KBytes- (portmap)
PID=3123 - Swap used: 136 KBytes- (rpc.statd)
PID=3151 - Swap used: 492 KBytes- (rpc.idmapd)
PID=3230 - Swap used: 228 KBytes- (dbus-daemon)
PID=3307 - Swap used: 88 KBytes- (acpid)
PID=3399 - Swap used: 164 KBytes- (nscd)
PID=3415 - Swap used: 7520 KBytes- (snmpd)
Share Button

Leave a comment

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