释放linux的swap

post by rocdk890 / 2015-5-22 18:17 Friday linux技术
  今天朋友说他自己的服务器上的mysql和tomcat老是自动重启,上去看了下swap都已经满了,但内存占用又不大,如下图:
点击查看原图
好吧,先试试内存释放有没有用吧:
  sync && echo 3 > /proc/sys/vm/drop_caches
没有释放到,好吧,那就只能用下面的方法了:
  swapoff -a

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

   swapon -a

点击查看原图

好了,这样就把swap给释放完整了.下面再给大家一个统计当前所有进程使用swap分区情况的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

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

ps:http://freeloda.blog.51cto.com/2033581/1221653
夜空- 本站版权
1、本站所有主题由该文章作者发表,该文章作者与夜空享有文章相关版权
2、其他单位或个人使用、转载或引用本文时必须同时征得该文章作者和夜空的同意
3、本帖部分内容转载自其它媒体,但并不代表本站赞同其观点和对其真实性负责
4、如本帖侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意
5、原文链接:blog.slogra.com/post-595.html

标签: 缓存 linux swap 释放

评论: