Skip to content

Instantly share code, notes, and snippets.

@nkwhr
Last active August 29, 2015 14:02
Show Gist options
  • Save nkwhr/6c94a2397645abe9cf9b to your computer and use it in GitHub Desktop.
Save nkwhr/6c94a2397645abe9cf9b to your computer and use it in GitHub Desktop.
A shellscript to check who's hogging swap.
#!/bin/sh
## e.g.) sudo ./swap_stats.sh | column -t | sort -nrk3 | head -n25"
if [ $UID -ne 0 ] ; then
echo "You must be root to run this script." 1>&2
exit 1
fi
TOTAL_SWAP=0
for PID in $(ls -1 /proc | grep ^[0-9]) ; do
PROG=$(ps -p $PID -o comm --no-heading)
SWAP=$(grep ^Swap /proc/$PID/smaps 2>/dev/null | awk 'BEGIN{SUM=0}{SUM+=$2}END{print SUM}')
if [ $SWAP -gt 0 ] ; then
echo "PID=$PID ($PROG) $SWAP KB"
TOTAL_SWAP=(expr $TOTAL_SWAP + $SWAP)
fi
done
echo "TOTAL --- $TOTAL_SWAP KB"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment