如何找出 Linux 中哪些进程正在使用交换空间?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/479953/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 16:54:44  来源:igfitidea点击:

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

linuxmemoryswap

提问by Shameem

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

在 Linux 下,如何找出哪个进程更多地使用交换空间?

采纳答案by David Holm

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

运行top然后按OpEnter。现在进程应该按它们的交换使用进行排序。

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:

这是更新,因为我的原始答案没有提供评论中指出的问题的确切答案。从htop 常见问题解答

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).

无法获得进程使用的交换空间的确切大小。Top 通过使 SWAP = VIRT - RES 来伪造这些信息,但这不是一个好的指标,因为其他东西,如视频内存也依赖于 VIRT(例如:top 说我的 X 进程正在使用 81M 的交换,但它也报告我的系统作为一个整体只使用了 2M 的交换。因此,我不会在 htop 中添加类似的交换列,因为我不知道获取此信息的可靠方法(实际上,我认为不可能获得一个确切的数字,因为共享页面)。

回答by dmckee --- ex-moderator kitten

I suppose you could get a good guess by running topand 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.

我想你可以通过运行top和查找使用大量内存的活动进程来得到一个很好的猜测。以编程方式执行此操作更难——看看关于 Linux OOM 杀手启发式的无休止的争论。

Swapping is a function of having more memory in activeuse 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.

交换是一种具有比安装更多内存处于活动状态的功能,因此通常很难将其归咎于单个进程。如果这是一个持续存在的问题,最好的解决方案是安装更多内存,或进行其他系统性更改。

回答by Jean Azzopardi

I don't know of any direct answer as how to find exactly what process is using the swap space, however, this link may be helpful. Another good one is over here

我不知道关于如何准确找到正在使用交换空间的进程的任何直接答案,但是,此链接可能会有所帮助。另一个好的在这里

Also, use a good tool like htop to see which processes are using a lot of memory and how much swap overall is being used.

另外,使用像 htop 这样的好工具来查看哪些进程使用了​​大量内存以及使用了多少交换空间。

回答by Ronny Vindenes

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 topand order by swap (press 'Op'), for the latter you can run vmstatand look for non-zero entries for 'so'.

对于第一个,您可以top通过交换运行和排序(按“Op”),对于后者,您可以运行vmstat并查找“so”的非零条目。

回答by lolotux

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

我找到的最好的脚本是在这个页面上:http: //northernmost.org/blog/find-out-what-is-using-your-swap/

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

这是脚本的一种变体,不需要 root:

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

回答by Alexis

On MacOSX, you run top command as well but need to type "o" then "vsize" then ENTER.

在 MacOSX 上,您也可以运行 top 命令,但需要输入“o”,然后是“vsize”,然后按 ENTER。

回答by Amol Kulkarni

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.

top 命令还包含一个字段,用于显示进程的页面错误数。页面错误最多的进程将是交换最多的进程。对于长时间运行的守护进程,它们可能会在开始时引发大量页面错误,而此后数量不会增加。所以我们需要观察页面错误是否在增加。

回答by j3nda

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):

这是脚本的另一个变体,但旨在提供更具可读性的输出(您需要以 root 身份运行它以获得准确的结果):

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


SCRIPT_NAME=`basename ##代码##`;
SORT="kb";                 # {pid|kB|name} as first parameter, [default: kb]
[ "" != "" ] && { SORT=""; }

[ ! -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  }'`
    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=0
done
echo "${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;
        ;;
esac
rm -fR "${TMP}/";

回答by Tom

I did notice this thread is rather old, but if you happen to stumble upon it, as I just did, another answer is: use smem.

我确实注意到这个线程很旧,但如果你碰巧偶然发现它,就像我刚刚做的那样,另一个答案是:使用 smem。

Here is a link which tells you both how to install it and how to use it:

这是一个链接,它告诉您如何安装它以及如何使用它:

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

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

回答by sunil

iotopis 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 -Pto get per process info. This is not available by default. You may have to install via rpm/apt.

iotop是一个非常有用的工具。它提供了每个进程/线程的 I/O 和交换使用情况的实时统计信息。默认情况下,它显示每个线程,但您可以iotop -P获取每个进程的信息。这在默认情况下不可用。您可能必须通过 rpm/apt 安装。