我的博客已迁移到xdoujiang.com请去那边和我交流
1、查看版本
cat /etc/debian_version 
7.8
2、看下内存(我这边是256MB内存 swap分了4G)
free -m
             total       used       free     shared    buffers     cached
Mem:           231        124        106          0          8         79
-/+ buffers/cache:         36        194
Swap:         3814          0       3814
3、使用stress模拟占用swap分区环境
stress --cpu 1 --io 1 --vm 2 --vm-bytes 25000000 --vm-keep --verbose

shell分析swap分区被哪些程序占用(stress模拟环境)_stress

4、查看到swap分区不断在使用

shell分析swap分区被哪些程序占用(stress模拟环境)_swap_02

5、使用脚本分析下
#!/bin/bash
#--------------------------------------------------  
#Created:2015-05-20
#Author:jimmygong
#Mail:jimmygong@taomee.com
#Function:
#Version:1.0
#--------------------------------------------------
function swapoccupancy ()
{
    echo -e "Pid\tSwap\tProgame"
    num=0
    for pid in `ls -1 /proc|egrep "^[0-9]"`
    do
        if [[ $pid -lt 20 ]]
        then
            continue
        fi
        program=`ps -eo pid,command|grep -w $pid|grep -v grep|awk '{print $2}'`
        for swap in `grep Swap /proc/$pid/smaps 2>/dev/null|awk '{print $2}'`
        do
            let num=$num+$swap
        done
        echo -e "${pid}\t${num}\t${program}"
        num=0
    done|sort -nrk2|head
}
swapoccupancy
exit 0
6、我这边脚本写的是取占用前10的

shell分析swap分区被哪些程序占用(stress模拟环境)_swap_03

7、提供下高手写的脚本
#!/bin/bash
cd /proc
for pid in [0-9]*; do
    command=$(cat /proc/$pid/cmdline)
    swap=$(
        awk '
            BEGIN  { total = 0 }
            /Swap/ { total += $2 }
            END    { print total }
        ' /proc/$pid/smaps
    )
    if (( $swap > 0 )); then
        if [[ "${head}" != "yes" ]]; then
            echo -e "PID\tSWAP\tCOMMAND"
            head="yes"
        fi
        echo -e "${pid}\t${swap}\t${command}"
    fi
done