[标签]:Linux
[说明]:Linux的内存机制跟windows有所不同。Camor仅仅是出于某种习惯,写下这么一个脚本。声明一下:本脚本简陋粗暴,不适用于某些预读文件到内存中进行加速的平台。是否有用,完全看自己体会。
# camor原创并首发于http://camor.cublog.cn# 转载和引用请先留言并注明出处!# [By camornow$gmail.com]
脚本包含变量、日志路径、内存清除机制等等。
设定环境:
CentOS 5.3
如果未特别指出,则以下所有指令均默认在root登陆状态下执行。
日志路径:/var/log/freemem/memstat_$Time1.log
脚本路径:/Datas/myscript/freemem.sh
废话不多说,详细脚本如下:
/Datas/myscript#vi freemem.sh
#
##########################
# Freemem by Camor
##########################
#!/bin/sh
Time1=`date +%Y%m%d`
Time2=`date +%k:%M:%S`
cd /var/log/freemem
#
#echo "">memstat_$Time1.log
#
echo $Time1--$Time2 >>memstat_$Time1.log
echo "------------------" >>memstat_$Time1.log
echo "The mem stat is :" >>memstat_$Time1.log
echo "" >>memstat_$Time1.log
free >>memstat_$Time1.log
echo "" >>memstat_$Time1.log
#sync
echo "....Freeing......" >>memstat_$Time1.log
echo "">>memstat_$Time1.log
#
echo 1 > /proc/sys/vm/drop_caches
echo 2 > /proc/sys/vm/drop_caches
echo 3 > /proc/sys/vm/drop_caches
echo "---> OK ! " >>memstat_$Time1.log
echo "">>memstat_$Time1.log
free >>memstat_$Time1.log
echo "=========================================================================">> memstat_$Time1.log
echo "">>memstat_$Time1.log
echo "">>memstat_$Time1.log
cd -
cat /var/log/freemem/memstat_$Time1.log
# 脚本结束
然后到/etc/cron.d/目录下新建freemem的轮询,内容如下:
#freemem
0 */8 * * * root /Datas/myscript/freemem.sh &>/dev/null
#这里我设定的是每8小时执行一次。可以根据实验效果修改。。
...ok.
执行效果如下(截自日志文件):
20100120-- 8:00:01
------------------
The mem stat is :
total used free shared buffers cached
Mem: 3367688 573432 2794256 0 167248 261236
-/+ buffers/cache: 144948 3222740
Swap: 2064312 0 2064312
....Freeing......
---> OK !
total used free shared buffers cached
Mem: 3367688 153744 3213944 0 652 36340
-/+ buffers/cache: 116752 3250936
Swap: 2064312 0 2064312
=========================================================================
阅读(2038) | 评论(0) | 转发(0) |