当前位置: > Linux教程 > 系统运维 >

释放linux的swap

时间:2016-06-05 00:24来源:linux.it.net.cn 作者:IT
  今天朋友说他自己的服务器上的mysql和tomcat老是自动重启,上去看了下swap都已经满了,但内存占用又不大,如下图:

好吧,先试试内存释放有没有用吧:
  sync && echo 3 > /proc/sys/vm/drop_caches
没有释放到,好吧,那就只能用下面的方法了:
  swapoff -a

ps:停止是需要一段时间的,swap将释放到实际内存当中.
释放完后,再执行

   swapon -a

好了,这样就把swap给释放完整了.下面再给大家一个统计当前所有进程使用swap分区情况的shell脚本:
01 #!/bin/bash
02 #
03 function getswap {
04 SUM=0
05 OVERALL=0
06 for DIR in `find /proc/ -maxdepth 1 -type d | egrep "^/proc/[0-9]"` ; do
07 PID=`echo $DIR | cut -d / -f 3`
08 PROGNAME=`ps -p $PID -o comm --no-headers`
09 for SWAP in `grep Swap $DIR/smaps 2>/dev/null| awk '{ print $2 }'`
10 do
11 let SUM=$SUM+$SWAP
12 done
13 echo "PID=$PID - Swap used: $SUM - ($PROGNAME )"
14 let OVERALL=$OVERALL+$SUM
15 SUM=0
16 done
17 echo "Overall swap used: $OVERALL"
18 }
19 getswap

效果如下:
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,好了,有兴趣的大家可以拿去试试.


(责任编辑:IT)
------分隔线----------------------------
栏目列表
推荐内容