1

I just ran into a swap problem, so I tried to find which process was using swap, with the script(getswap.sh) shown in this. It was php-fpm, about 200 subprocess, either toke 1M swap space. So I killed php-fpm. Then I ran the script again, and total swap used decreased a lot. Howerver, result in free -m only decreased for about 3M. What is the problem?

before killing php-fpm:

[root@eng /tmp]# bash getswap.sh | sort -n -k5>out
[root@eng /tmp]# cat out|awk '{a+=$5;}END{print a;}'
202076
[root@eng /tmp]# free -m
             total       used       free     shared    buffers     cached
Mem:         64259      60566       3692          0        192      17098
-/+ buffers/cache:      43275      20983
Swap:         4095        155       3940

after killing php-fpm:

[root@eng /tmp]# bash getswap.sh | sort -n -k5>out
[root@eng /tmp]# cat out|awk '{a+=$5;}END{print a;}'
108456
[root@eng /tmp]# free -m
             total       used       free     shared    buffers     cached
Mem:         64259      60402       3857          0        192      17043
-/+ buffers/cache:      43166      21092
Swap:         4095        152       3943

and the script:

#!/bin/bash
function getswap {
SUM=0
OVERALL=0
for DIR in `find /proc/ -maxdepth 1 -type d | egrep "^/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"
}
getswap

thx in advance

Community
  • 1
  • 1
chenming
  • 31
  • 2
  • You do realize that you're getting numbers in `kB` from smaps, while you're getting numbers in `mB` from `free -m`? at least get the values scaled the same so you can *attempt* to compare them. – Anya Shenanigans Jun 23 '15 at 15:34
  • I forgot to say that I ran this 2 command/script then compared them in reasonable way, not just use == to compare them in code. – chenming Jun 25 '15 at 12:32
  • Show your numbers in a form that can be compared easily - highlight where you determine the difference and why it looks wrong. What I'm suggesting is you divide the number from your smaps calculation by 1024, and use that for the comparison, as its only works out at a 91 mB smaps delta which is well within noise bounds of the values from free. – Anya Shenanigans Jun 26 '15 at 18:12
  • `smaps` only accounts process (as well as tools like `smem`); `free` (as `/proc/swaps` and tools such as `vmstat` or `top`) also include the **[Page cache](https://www.thomas-krenn.com/en/wiki/Linux_Page_Cache_Basics)** which could account for some of the diff (e.g. process binaries and other files still in the kernel Page cache). – tuk0z Jun 22 '16 at 18:04

0 Answers0