前言

若操作系统的物理内存用完了,则就会用到swap(虚拟内存)。系统就会跑得很慢,但仍能运行;如果swap分区用完了,那么系统就会发生错误。通常会出现“Application is out of memory”的错误,严重时会造成服务进程的死锁。操作系统无法正常运行,所以我们要高度重视内存使用,下面我们就来说一说,怎么统计哪些进程使用了swap分区,查看swap的方法有许多,下面我们就来一个个说明!

1. free 命令

free

free –m 命令中只能查看当前swap分区的使用情况,但不能查看具体哪些进程使用了swap!

2. top 命令

CentOS 5.5 (top)

top

top 命令能查看swap总量和使用情况,我们还可以使用快捷键查看,top+f+p来查看!

f

p

并不是实际的使用swap,而是VIRT-RES得来的,就是虚拟内存中所使用过的swap部分,并不能得到我们想要的结果!

CentOS 6.0-6.4 (top)

6.0

这样就明显看出是取出的每个进程的swap,能很方便的查看哪些进程使用了swap。从中也能看到一个信息,那就是读取了/proc/#/status !

3. vmstat  命令

vmstat

vmstat 也不能查看哪个进程使用了swap,我们只能看到,swap --- si 与 so,下面我们简单说明一下!

Memory(内存):

  • swpd: 使用虚拟内存大小

  • free: 可用内存大小

  • buff: 用作缓冲的内存大小

  • cache: 用作缓存的内存大小

Swap (虚拟内存):

  • si: 每秒从交换区读到内存的大小

  • so: 每秒写入交换区的内存大小

4. shell 查看

在Linux内核 2.6.16中引入了一个系统内存接口特性,这个接口位于/proc/$pid/目录下的smaps文件中 ,一看内容发现是进程内存映像信息,比同一目录下的maps文件更详细些!

[root@localhost ~]# cat /proc/1/smaps

proc

下面我们简单说明一下samps里面的内容,

  • 00400000-00409000 是该虚拟内存段的开始和结束位置

  • r-xp 内存段的权限,rw是指可读写,x是指可执行,p是指私有,如果是s则为共享

  • 00000000 该虚拟内存段在对应的映射文件中的偏移量

  • 08:02 文件的主设备和次设备号

  • 1361644 被映射到虚拟内存的文件的索引节点号

  • /sbin/init 被映射到虚拟内存的文件名称

  • Size 是进程使用内存空间,并不一定实际分配了内存(VSS)

  • Rss 是实际分配的内存

  • Shared_Clean 和其他进程共享的未改写页面

  • Shared_Dirty 和其他进程共享的已改写页面

  • Private_Clean 未改写的私有页面页面

  • Private_Dirty 已改写的私有页面页面

  • Swap 存在于交换分区的数据大小(如果物理内存有限,可能存在一部分在主存一部分在交换分区)

  • Pss是平摊计算后的使用内存(有些内存会和其他进程共享,例如mmap进来的)

通过 cat /proc/$(pid)/smaps 能查看所有进程的使用情况,若你想要查看某个进程所使用的swap只需要,

awk '/^Swap:/ {SWAP+=$2}END{print SWAP" KB"}' /proc/$(pid)/smaps


5. shell 脚本统计

#!/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

效果:

[root@localhost ~]# sh getswap.sh
PID=1 - Swap used: 76 - (init )
PID=2 - Swap used: 0 - (migration/0 )
PID=3 - Swap used: 0 - (ksoftirqd/0 )
PID=4 - Swap used: 0 - (events/0 )
PID=5 - Swap used: 0 - (khelper )
PID=7 - Swap used: 0 - (kthread )
PID=11 - Swap used: 0 - (kblockd/0 )
PID=12 - Swap used: 0 - (kacpid )
PID=177 - Swap used: 0 - (cqueue/0 )
PID=180 - Swap used: 0 - (khubd )
PID=182 - Swap used: 0 - (kseriod )
PID=250 - Swap used: 0 - (khungtaskd )
PID=251 - Swap used: 0 - (pdflush )
PID=252 - Swap used: 0 - (pdflush )
PID=253 - Swap used: 0 - (kswapd0 )
PID=254 - Swap used: 0 - (aio/0 )
PID=460 - Swap used: 0 - (kpsmoused )
PID=490 - Swap used: 0 - (mpt_poll_0 )
PID=491 - Swap used: 0 - (mpt/0 )
PID=492 - Swap used: 0 - (scsi_eh_0 )
PID=495 - Swap used: 0 - (ata/0 )
PID=496 - Swap used: 0 - (ata_aux )
PID=503 - Swap used: 0 - (kstriped )
PID=512 - Swap used: 0 - (kjournald )
PID=537 - Swap used: 0 - (kauditd )
PID=570 - Swap used: 460 - (udevd )
PID=1827 - Swap used: 0 - (kmpathd/0 )
PID=1828 - Swap used: 0 - (kmpath_handlerd )
PID=1889 - Swap used: 0 - (kjournald )
PID=1891 - Swap used: 0 - (kjournald )
PID=2361 - Swap used: 512 - (dhclient )
PID=2420 - Swap used: 228 - (auditd )
PID=2422 - Swap used: 128 - (audispd )
PID=2452 - Swap used: 16 - (syslogd )
PID=2456 - Swap used: 84 - (klogd )
PID=2535 - Swap used: 120 - (portmap )
PID=2565 - Swap used: 0 - (rpciod/0 )
PID=2571 - Swap used: 140 - (rpc.statd )
PID=2603 - Swap used: 464 - (rpc.idmapd )
PID=2626 - Swap used: 368 - (dbus-daemon )
PID=2639 - Swap used: 128 - (hcid )
PID=2643 - Swap used: 100 - (sdpd )
PID=2664 - Swap used: 0 - (krfcommd )
PID=2707 - Swap used: 656 - (pcscd )
PID=2721 - Swap used: 104 - (acpid )
PID=2735 - Swap used: 2196 - (hald )
PID=2736 - Swap used: 184 - (hald-runner )
PID=2745 - Swap used: 112 - (hald-addon-acpi )
PID=2756 - Swap used: 116 - (hald-addon-keyb )
PID=2765 - Swap used: 84 - (hald-addon-stor )
PID=2789 - Swap used: 104 - (hidd )
PID=2813 - Swap used: 260 - (automount )
PID=2849 - Swap used: 564 - (sshd )
PID=2862 - Swap used: 768 - (cupsd )
PID=2906 - Swap used: 1108 - (sendmail )
PID=2914 - Swap used: 1144 - (sendmail )
PID=2928 - Swap used: 80 - (gpm )
PID=2941 - Swap used: 508 - (crond )
PID=2964 - Swap used: 276 - (xfs )
PID=2989 - Swap used: 104 - (atd )
PID=3015 - Swap used: 80 - (avahi-daemon )
PID=3016 - Swap used: 156 - (avahi-daemon )
PID=3044 - Swap used: 200 - (smartd )
PID=3047 - Swap used: 320 - (login )
PID=3048 - Swap used: 68 - (mingetty )
PID=3049 - Swap used: 72 - (mingetty )
PID=3050 - Swap used: 68 - (mingetty )
PID=3051 - Swap used: 68 - (mingetty )
PID=3052 - Swap used: 72 - (mingetty )
PID=3103 - Swap used: 15148 - (yum-updatesd )
PID=3105 - Swap used: 160 - (gam_server )
PID=3106 - Swap used: 416 - (bash )
PID=3215 - Swap used: 728 - (sshd )
PID=3217 - Swap used: 356 - (bash )
PID=3249 - Swap used: 728 - (sshd )
PID=3251 - Swap used: 356 - (bash )
PID=3280 - Swap used: 616 - (sshd )
PID=3282 - Swap used: 140 - (bash )
PID=11634 - Swap used: 0 - (sh )
PID=11635 - Swap used: 0 - ( )
PID=11636 - Swap used: 0 - ( )
PID=11637 - Swap used: 0 - ( )
Overall swap used: 30944

单位是KB,好了,完成全部测试!^_^ ……