How to find out which processes are using swap space in Linux? How to find out which processes are using swap space in Linux? linux linux

How to find out which processes are using swap space in Linux?


The best script I found is on this page : http://northernmost.org/blog/find-out-what-is-using-your-swap/

Here's one variant of the script and no root needed:

#!/bin/bash # Get current swap usage for all running processes# Erik Ljungstrom 27/05/2011# Modified by Mikko Rantalainen 2012-08-09# Pipe the output to "sort -nk3" to get sorted output# Modified by Marc Methot 2014-09-18# removed the need for sudoSUM=0OVERALL=0for DIR in `find /proc/ -maxdepth 1 -type d -regex "^/proc/[0-9]+"`do    PID=`echo $DIR | cut -d / -f 3`    PROGNAME=`ps -p $PID -o comm --no-headers`    for SWAP in `grep VmSwap $DIR/status 2>/dev/null | awk '{ print $2 }'`    do        let SUM=$SUM+$SWAP    done    if (( $SUM > 0 )); then        echo "PID=$PID swapped $SUM KB ($PROGNAME)"    fi    let OVERALL=$OVERALL+$SUM    SUM=0doneecho "Overall swap used: $OVERALL KB"


Run top then press OpEnter. Now processes should be sorted by their swap usage.

Here is an update as my original answer does not provide an exact answer to the problem as pointed out in the comments. From the htop FAQ:

It is not possible to get the exact size of used swap space of a process. Top fakes this information by making SWAP = VIRT - RES, but that is not a good metric, because other stuff such as video memory counts on VIRT as well (for example: top says my X process is using 81M of swap, but it also reports my system as a whole is using only 2M of swap. Therefore, I will not add a similar Swap column to htop because I don't know a reliable way to get this information (actually, I don't think it's possible to get an exact number, because of shared pages).


Here's another variant of the script, but meant to give more readable output (you need to run this as root to get exact results):

#!/bin/bash    # find-out-what-is-using-your-swap.sh    # -- Get current swap usage for all running processes    # --    # -- rev.0.3, 2012-09-03, Jan Smid          - alignment and intendation, sorting    # -- rev.0.2, 2012-08-09, Mikko Rantalainen - pipe the output to "sort -nk3" to get sorted output    # -- rev.0.1, 2011-05-27, Erik Ljungstrom   - initial versionSCRIPT_NAME=`basename $0`;SORT="kb";                 # {pid|kB|name} as first parameter, [default: kb][ "$1" != "" ] && { SORT="$1"; }[ ! -x `which mktemp` ] && { echo "ERROR: mktemp is not available!"; exit; }MKTEMP=`which mktemp`;TMP=`${MKTEMP} -d`;[ ! -d "${TMP}" ] && { echo "ERROR: unable to create temp dir!"; exit; }>${TMP}/${SCRIPT_NAME}.pid;>${TMP}/${SCRIPT_NAME}.kb;>${TMP}/${SCRIPT_NAME}.name;SUM=0;OVERALL=0;    echo "${OVERALL}" > ${TMP}/${SCRIPT_NAME}.overal;for DIR in `find /proc/ -maxdepth 1 -type d -regex "^/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 > 0 ));    then        echo -n ".";        echo -e "${PID}\t${SUM}\t${PROGNAME}" >> ${TMP}/${SCRIPT_NAME}.pid;        echo -e "${SUM}\t${PID}\t${PROGNAME}" >> ${TMP}/${SCRIPT_NAME}.kb;        echo -e "${PROGNAME}\t${SUM}\t${PID}" >> ${TMP}/${SCRIPT_NAME}.name;    fi    let OVERALL=$OVERALL+$SUM    SUM=0doneecho "${OVERALL}" > ${TMP}/${SCRIPT_NAME}.overal;echo;echo "Overall swap used: ${OVERALL} kB";echo "========================================";case "${SORT}" in    name )        echo -e "name\tkB\tpid";        echo "========================================";        cat ${TMP}/${SCRIPT_NAME}.name|sort -r;        ;;    kb )        echo -e "kB\tpid\tname";        echo "========================================";        cat ${TMP}/${SCRIPT_NAME}.kb|sort -rh;        ;;    pid | * )        echo -e "pid\tkB\tname";        echo "========================================";        cat ${TMP}/${SCRIPT_NAME}.pid|sort -rh;        ;;esacrm -fR "${TMP}/";