centos 5 释放内存的方法

14 篇文章 0 订阅
1 篇文章 0 订阅

这种释放方法是相当地痛快,而且如果真是non-destructive operation的话那就太好了,jvm 的内存占用一下子减少了接近300m.

#sync

To free pagecache, dentries and inodes:

#echo 3 > /proc/sys/vm/drop_caches


This is a non-destructive operation and will only free things that are completely unused. Dirty objects will continue to be in use until written out to disk and are not freeable. If you run "sync" first to flush them out to disk, these drop operations will tend to free more memory.


再说说 free 命令

# free -m
             total       used       free     shared    buffers     cached
Mem:           497        438         59          0         20        325
-/+ buffers/cache:         92        404
Swap:          511         22        489


其中:
total 内存总数
used 已经使用的内存数
free 空闲的内存数
shared 多个进程共享的内存总额
buffers buffer Cache和cached Page Cache 磁盘缓存的大小
-buffers/cache (已用)的内存数:used - buffers - cached
+buffers/cache(可用)的内存数:free + buffers + cached
可用的memory=free memory+buffers+cached


下面包含一个自动释放内存的脚本,网上学习的, 加入了crondtab计划任务了

# vim /root/shTools/freemem.sh

#!/bin/bash

used=`free -m | awk 'NR==2' | awk '{print $3}'`
free=`free -m | awk 'NR==2' | awk '{print $4}'`

echo "===========================" >> /var/log/mem.log
date >> /var/log/mem.log
echo "Memory usage | [Use:${used}MB][Free:${free}MB]" >> /var/log/mem.log

if [ $free -le 100 ] ; then
                sync && echo 1 > /proc/sys/vm/drop_caches
                sync && echo 2 > /proc/sys/vm/drop_caches
                sync && echo 3 > /proc/sys/vm/drop_caches
                echo "OK" >> /var/log/mem.log
else
                echo "Not required" >> /var/log/mem.log

将脚本添加到crond任务,定时执行。
# echo "*/30 * * * * root /root/shTools/freemem.sh" >> /etc/crondtab



Referrence:

http://bbs.chinaunix.net/thread-3580815-1-1.html

http://www.linuxde.net/2011/07/402.html


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值