Friday, September 16, 2011

How to find out which process is using swap space?

■ Requirement :  Find out process which consumes swap space
■ OS Environment : Linux, RHEL, Centos
■ Implementation Steps : 

1. If we would like to sort out the running or queueing process as per swap usage we can do like :

$ top

Then press capital "o" (ie "O") followed by "p" and press enter. Now processes should be sorted by their swap usage.

2. Use script : 
Use bash script to pick up the process from /proc file system.

#!/bin/bash
# Get current swap usage for all running processes
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 )"
let OVERALL=$OVERALL+$SUM
SUM=0

done
echo "Overall swap used: $OVERALL"

No comments:

Post a Comment