什么以及为什么我的交换空间在 linux 下使用

Fab*_*ian 5 linux swap

在我的 linux 系统上,我从顶部获取这些统计信息:

Tasks: 155 total,   1 running, 153 sleeping,   0 stopped,   1 zombie
Cpu(s):  1.5%us,  0.3%sy,  0.0%ni, 97.4%id,  0.7%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:   8177180k total,  2025504k used,  6151676k free,    44176k buffers
Swap:  7999996k total,   495300k used,  7504696k free,   637612k cached
Run Code Online (Sandbox Code Playgroud)

它显示我的系统正在使用 495Mb 的交换。为什么会这样?6Gigs 的 ram 是免费的。如果我完全禁用交换,系统也可以工作。

任何解释数字真正显示的内容或谁在交换?

Der*_*rfK 8

即使没有应用程序对您的内存的需求,Linux 也会“提前”换出实际需要的进程中未使用的部分,以便它可以在时机成熟时立即释放该内存。您可以按照此处的说明通过调整vm.swappiness( /proc/sys/vm/swappiness)来调整执行此操作的趋势。

至于查看交换的内容,理论上您可以从输出中看出top(通过减去虚拟和常驻内存列,或使用对您执行相同操作的交换列),但我的系统使用了 0 个交换和一个 apache2处理 248m“虚拟映像”,其中 9376k 据称是“常驻”,留下 239m“交换”。我不确定是否有一种实际的方法来识别交换文件中实际存在哪些特定进程或进程的一部分。


ari*_*ica 7

这是一个脚本,用于向您展示 Linux 系统上进程使用的交换。

感谢原作者:Erik Ljungstrom 27/05/2011。

由我修改以增加实用性和友好性。哈。

#!/bin/bash
#
# Get current swap usage for running processes
# Original: Erik Ljungstrom 27/05/2011
# Modifications by ariel:
#   - Sort by swap usage
#   - Auto run as root if not root
#   - ~2x speedup by using procfs directly instead of ps
#   - include full command line in output
#   - make output more concise/clearer
#   - better variable names
#

# Need root to look at all processes details
case `whoami` in
    'root') : ;;
    *) exec sudo $0 ;;
esac

(
    PROC_SWAP=0
    TOTAL_SWAP=0

    for DIR in `find /proc/ -maxdepth 1 -type d | grep "^/proc/[0-9]"` ; do
        PID=`echo $DIR | cut -d / -f 3`
        CMDLINE=`cat /proc/$PID/cmdline 2>/dev/null | tr '\000' ' '`
        for SWAP in `grep Swap $DIR/smaps 2>/dev/null | awk '{ print $2 }'`
        do
            let PROC_SWAP=$PROC_SWAP+$SWAP
        done
        if [ $PROC_SWAP == 0 ]; then
            # Skip processes with no swap usage
            continue
        fi
        echo "$PROC_SWAP        [$PID] $CMDLINE"
        let TOTAL_SWAP=$TOTAL_SWAP+$PROC_SWAP
        PROC_SWAP=0
    done
    echo "$TOTAL_SWAP   Total Swap Used"
) | sort -n
Run Code Online (Sandbox Code Playgroud)


Luc*_*man 0

交换用于内存中不经常访问的页面,即使您有大量空闲内存,它仍然会交换一些程序。这在一定程度上是为了避免在装满时必须进行交换。否则,您会突然请求大量内存,然后您的操作系统应该开始交换,然后才能将该内存提供给您的程序。这些页面没有被使用,因此它们不必位于 RAM 中,因此它们会被交换。