在Linux下,如何发现哪个进程使用交换空间更多?


当前回答

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

其他回答

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

这是一个更新,因为我原来的答案没有提供一个确切的答案,在评论中指出的问题。从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).

我不知道如何确切地找到哪个进程正在使用交换空间的直接答案,但是,这个链接可能会有帮助。另一个好例子在这里

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

使用smem

smem -s swap -r

这里有一个链接,告诉你如何安装和如何使用它:http://www.cyberciti.biz/faq/linux-which-process-is-using-swap/

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

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

这里是脚本的一个变体,不需要根:

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