天天看點

centos 5 釋放記憶體的方法

這種釋放方法是相當地痛快,而且如果真是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

繼續閱讀