5

I am trying to find out what exactly uses swap memory in my system. Free reports over 5GB of used swap memory, but all processes use only 24MB of swap. I suspect that swap might be used by a tmpfs filesystem, but I don't know how to check it.

Would you be so kind to tell what else should I check to examine content of the swap memory, please?

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

    # 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
    
  • tmpfs

    # 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
    

EDIT:

Problem solved, I found an information that 1372500 pages of shared memory is swapped:

    # 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
gbajson
  • 191
  • 1
  • 8
  • Instead of an edit, you could add your solution as an answer. This way anyone would know that this already has an answer before reading to the bottom. – Esa Jokinen Jun 09 '17 at 08:20

4 Answers4

3

Swap usage per proccess

#!/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"
Aaron
  • 2,809
  • 2
  • 11
  • 29
  • I tried it at the very beginning, but it only show memory used by processes: `# grep "VmSwap:" /proc/*/status | awk '{swapped+=$2} END {print swapped/1024" MB"}' 23.8438 MB` – gbajson Jun 16 '17 at 14:01
2

Problem solved:

I found out that 1372500 pages, of shared memory is swapped. Size of the page is 4096 bytes.

# 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
gbajson
  • 191
  • 1
  • 8
1

Linux moves infrequently accessed memory to swap; it doesn't always matter whether you have free memory or not at the time, see this answer on a similar question at askubuntu: https://askubuntu.com/a/159358

Ricky
  • 81
  • 5
0

I use this bash to see what processes in swap - for file in /proc/*/status ; do awk '/VmSwap|Name/{printf $2 " " $3}END{ print ""}' $file; done | sort -k 2 -n -r | less

Alexander Tolkachev
  • 4,513
  • 3
  • 14
  • 23