Article by Erik Ljungstrom, published on http://northernmost.org/
Have you ever logged in to a server, ran `free`, seen that a bit of swap is used and wondered what’s in there? It’s usually not very indicative of anything, or even overly helpful knowing what’s in there, mostly it’s a curiosity thing.
Either way, starting from kernel 2.6.16, we can find out using smaps which can be found in the proc filesystem. I’ve written a simple bash script which prints out all running processes and their swap usage.
It’s quick and dirty, but does the job and can easily be modified to work on any info exposed in /proc/$PID/smaps
If I find the time and inspiration, I might tidy it up and extend it a bit to cover some more alternatives. The output is in kilobytes.
#!/bin/bash
# Get current swap usage for all running processes
# Erik Ljungstrom 27/05/2011
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"
This will need to be ran as root for it to be able to gather accurate numbers. It will still work even if you don’t, but it will report 0 for any processes not owned by your user.
Needless to say, it’s Linux only. The output is ordered alphabetically according to your locale (which admittedly isn’t a great thing since we’re dealing with numbers), but you can easily apply your standard shell magic to the output. For instance, to find the process with most swap used, just run the script like so:
$ ./getswap.sh | sort -n -k 5
Don’t want to see stuff that’s not using swap at all?
$ ./getswap.sh | egrep -v "Swap used: 0" |sort -n -k 5
… and so on and so forth
How about a sample of the output?
Very useful script, 10x!
This seems to not work as advertised. I tested it on several SLES 10/11 servers and the output was always Swap used: 0, even when swap was in use.
Tested on Ubuntu 11.04 and Debian 6 and on both it works fine.
I’ll try to test on a Sles.
On my Ubuntu system this script says total swap used is 510MB, but ‘free’ output says it is 366MB used. Anyone tried to understand why they are different?
Guess I’ve got too much RAM: Overall swap used: 0… either that or none of my servers are popular enough to start swapping ;). Haha, anyways, thanks for the useful script, I do swap from time to time, and the script is useful if only for the lesson it teaches.
Please understand that I am by no means a bash master, but I have modified your script to omit the processes that are not used at all with out using grep.
#!/bin/bash
# Get current swap usage for all running processes
# Original: Erik Ljungstrom 27/05/2011
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})"
fi
let OVERALL=${OVERALL}+${SUM}
SUM=0
done
echo "Overall swap used: ${OVERALL}"
Here is the sample output (Some processes named omitted):
PID=1 - Swap used: 36 - (init)
PID=421 - Swap used: 320 - (****)
PID=821 - Swap used: 260 - (****)
PID=826 - Swap used: 268 - (****)
PID=1106 - Swap used: 136 - (****)
PID=1145 - Swap used: 5812 - (****)
PID=1779 - Swap used: 68 - (****)
Overall swap used: 6900
Works fine with Slackware 13.37, thanks for sharing this.
simple and good one