Search code examples
linuxmemoryswap

How can I know which process is using swap?


There is lots of memory avaiable(about 4G) but swap is used(200+M) in my fedora box.

I wonder which process is using swap. How can I know it.

ps and top only show the memory usage.

Thanks in advance.


Solution

  • From here:

    [a] /proc/meminfo - This file reports statistics about memory usage on the system. It is used by free to report the amount of free and used memory (both physical and swap) on the system as well as the shared memory and buffers used by the kernel. You can also use free, vmstat and other tools to find out the same information.

    [b] /proc/${PID}/smaps, /proc/${PID}/status, and /proc/${PID}/stat : Use these files to find information about memory, pages and swap used by each process using its PID.

    [c] smem - This command (python script) reports memory usage with shared memory divided proportionally.

    Also you can refer Find out what is using your swap

    #!/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"