编辑:

编辑:

我正在尝试找出系统中究竟是什么使用了交换内存。Free 报告已使用超过 5GB 的交换内存,但所有进程仅使用 24MB 的交换内存。我怀疑交换内存可能由 tmpfs 文件系统使用,但我不知道如何检查。

您能否告诉我还应该检查什么来检查交换内存的内容?

    # free -m
                 total       used       free     shared    buffers     cached
    Mem:         48206      47688        518       8177         10      11789
    -/+ buffers/cache:      35888      12317
    Swap:        15999       5378      10621
  • 来自 /proc/*/status 的 VmSwap

    # grep "VmSwap:" /proc/*/status | awk '{swapped+=$2} END {print swapped/1024" MB"}'
    23.8438 MB
    
    # grep "VmSwap:" /proc/*/status | grep -v " 0 kB"
    /proc/1/status:VmSwap:        52 kB
    /proc/27714/status:VmSwap:           956 kB
    /proc/27718/status:VmSwap:           948 kB
    /proc/27722/status:VmSwap:           820 kB
    /proc/27723/status:VmSwap:           804 kB
    /proc/27724/status:VmSwap:           812 kB
    /proc/27725/status:VmSwap:           804 kB
    /proc/29636/status:VmSwap:           760 kB
    /proc/29640/status:VmSwap:           960 kB
    /proc/29642/status:VmSwap:          1948 kB
    /proc/29643/status:VmSwap:           936 kB
    /proc/29647/status:VmSwap:          1624 kB
    /proc/29649/status:VmSwap:           872 kB
    /proc/3750/status:VmSwap:            116 kB
    /proc/3770/status:VmSwap:            228 kB
    /proc/3790/status:VmSwap:            260 kB
    /proc/3810/status:VmSwap:            444 kB
    /proc/3823/status:VmSwap:            164 kB
    /proc/4022/status:VmSwap:           2296 kB
    /proc/4037/status:VmSwap:            640 kB
    /proc/4049/status:VmSwap:            572 kB
    /proc/4058/status:VmSwap:            592 kB
    /proc/4068/status:VmSwap:            260 kB
    /proc/4081/status:VmSwap:            192 kB
    /proc/4095/status:VmSwap:             56 kB
    /proc/4110/status:VmSwap:             76 kB
    /proc/4112/status:VmSwap:             80 kB
    /proc/4114/status:VmSwap:             76 kB
    /proc/4116/status:VmSwap:             80 kB
    /proc/4118/status:VmSwap:             80 kB
    /proc/4120/status:VmSwap:             76 kB
    /proc/4127/status:VmSwap:           2068 kB
    /proc/4128/status:VmSwap:           2068 kB
    /proc/7655/status:VmSwap:            312 kB
    /proc/7661/status:VmSwap:            380 kB
    /proc/905/status:VmSwap:             840 kB
    
  • 临时文件

    # mount | grep tmpfs
    tmpfs on /dev/shm type tmpfs (rw,rootcontext="system_u:object_r:tmpfs_t:s0")
    
  • /proc/meminfo

    # cat /proc/meminfo           
    MemTotal:       49363344 kB
    MemFree:          700676 kB
    Buffers:           11000 kB
    Cached:         11822364 kB
    SwapCached:       522436 kB
    Active:         37945008 kB
    Inactive:        4986964 kB
    Active(anon):   35707220 kB
    Inactive(anon):  3720092 kB
    Active(file):    2237788 kB
    Inactive(file):  1266872 kB
    Unevictable:           0 kB
    Mlocked:               0 kB
    SwapTotal:      16383992 kB
    SwapFree:       10875936 kB
    Dirty:            224072 kB
    Writeback:             0 kB
    AnonPages:      30580752 kB
    Mapped:         12218972 kB
    Shmem:           8328572 kB
    Slab:             270104 kB
    SReclaimable:     181768 kB
    SUnreclaim:        88336 kB
    KernelStack:        4256 kB
    PageTables:        91528 kB
    NFS_Unstable:          0 kB
    Bounce:                0 kB
    WritebackTmp:          0 kB
    CommitLimit:    41065664 kB
    Committed_AS:   49589524 kB
    VmallocTotal:   34359738367 kB
    VmallocUsed:     5203000 kB
    VmallocChunk:   34327589304 kB
    HardwareCorrupted:     0 kB
    AnonHugePages:   5289984 kB
    HugePages_Total:       0
    HugePages_Free:        0
    HugePages_Rsvd:        0
    HugePages_Surp:        0
    Hugepagesize:       2048 kB
    DirectMap4k:    31952556 kB
    DirectMap2M:    17270784 kB
    DirectMap1G:     1048576 kB
    

编辑:

问题解决了,找到一个信息,1372500页共享内存被交换了:

    # ipcs -mu

    ------ Shared Memory Status --------
    segments allocated 32
    pages allocated 2752532
    pages resident  1380020
    pages swapped   1372500
    Swap performance: 0 attempts     0 successes

    # getconf PAGESIZE
    4096
    # echo $((1372500*4096/1024/1024))
    5361

答案1

每个进程的交换使用量

#!/bin/bash
set -o posix
set -u
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 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"

答案2

问题解决了:

我发现有 1372500 页共享内存被交换。页面大小为 4096 字节。

# ipcs -mu

------ Shared Memory Status --------
segments allocated 32
pages allocated 2752532
pages resident  1380020
pages swapped   1372500
Swap performance: 0 attempts     0 successes

# getconf PAGESIZE
4096

# echo "$((1372500*4096/1024/1024)) MB"
5361 MB

答案3

Linux 将不经常访问的内存移至交换区;此时您是否有可用内存并不重要,请参阅 askubuntu 上类似问题的答案:https://askubuntu.com/a/159358

答案4

我使用这个 bash 来查看交换-for file in /proc/*/status ; do awk '/VmSwap|Name/{printf $2 " " $3}END{ print ""}' $file; done | sort -k 2 -n -r | less

相关内容