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

后端 未结 18 1875
终归单人心
终归单人心 2020-11-27 09:11

Under Linux, how do I find out which process is using the swap space more?

相关标签:
18条回答
  • 2020-11-27 09:17

    iotop is a very useful tool. It gives live stats of I/O and swap usage per process/thread. By default it shows per thread but you can do iotop -P to get per process info. This is not available by default. You may have to install via rpm/apt.

    0 讨论(0)
  • 2020-11-27 09:18

    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 sudo
    
    SUM=0
    OVERALL=0
    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 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=0
    done
    echo "Overall swap used: $OVERALL KB"
    
    0 讨论(0)
  • 2020-11-27 09:19

    It's not entirely clear if you mean you want to find the process who has most pages swapped out or process who caused most pages to be swapped out.

    For the first you may run top and order by swap (press 'Op'), for the latter you can run vmstat and look for non-zero entries for 'so'.

    0 讨论(0)
  • 2020-11-27 09:19

    I suppose you could get a good guess by running top and looking for active processes using a lot of memory. Doing this programatically is harder---just look at the endless debates about the Linux OOM killer heuristics.

    Swapping is a function of having more memory in active use than is installed, so it is usually hard to blame it on a single process. If it is an ongoing problem, the best solution is to install more memory, or make other systemic changes.

    0 讨论(0)
  • 2020-11-27 09:21

    Gives totals and percentages for process using swap

    smem -t -p
    

    Source : https://www.cyberciti.biz/faq/linux-which-process-is-using-swap/

    0 讨论(0)
  • 2020-11-27 09:28

    The top command also contains a field to display the number of page faults for a process. The process with maximum page faults would be the process which is swapping most. For long running daemons it might be that they incur large number of page faults at the beginning and the number does not increase later on. So we need to observe whether the page faults is increasing.

    0 讨论(0)
提交回复
热议问题