在服务器运行过程中,使用free -m查看服务器内存时,经常会发现free值很小,有些同学就会很紧张,总想采取一些措施,使得free值看起来高一点,心里就比较爽一点。其实,个人觉得这完全是图一时之快,没什么实质性用途。
1. 首先使用free -m查看剩余内存 view plaincopy to clipboardprint? linux-8v2i:~ # free -m total used free shared buffers cached Mem: 3952 2773 178 0 130 1097 -/+ buffers/cache: 1545 2406 Swap: 2055 0 2055 linux-8v2i:~ # free -m total used free shared buffers cached Mem: 3952 2773 178 0 130 1097 -/+ buffers/cache: 1545 2406 Swap: 2055 0 2055
使用sync命令以确保文件系统的完整性,sync 命令运行 sync 子例程,将所有未写的系统缓冲区写到磁盘中,包含已修改的 i-node、已延迟的块 I/O 和读写映射文件。 view plaincopy to clipboardprint? linux-8v2i:~ # sync linux-8v2i:~ # sync
view plaincopy to clipboardprint? echo 3 > /proc/sys/vm/drop_caches echo 3 > /proc/sys/vm/drop_caches
说明: 1>. /proc是一个虚拟文件系统,我们可以通过对它的读写操作作为与kernel实体间进行通信的一种手段。也就是说可以通过修改/proc中的文件,来对当前kernel的行为做出调整。也就是说我们可以通过调整/proc/sys/vm/drop_caches来释放内存。
To free pagecache, use echo 1 > /proc/sys/vm/drop_caches; to free dentries and inodes, use echo 2 > /proc/sys/vm/drop_caches; to free pagecache, dentries and inodes, use echo 3 >/proc/sys/vm/drop_caches. Because this is a non-destructive operation and dirty objects are not freeable, the user should run sync first.
view plaincopy to clipboardprint? linux-8v2i:~ # free -m total used free shared buffers cached Mem: 3952 1773 2325 0 0 80 -/+ buffers/cache: 1545 2406 Swap: 2055 0 2055 linux-8v2i:~ # free -m total used free shared buffers cached Mem: 3952 1773 2325 0 0 80 -/+ buffers/cache: 1545 2406 Swap: 2055 0 2055
1. 首先对free -m查看结果进行分析 view plaincopy to clipboardprint? linux-8v2i:~ # free -m total used free shared buffers cached Mem: 3952 2773 178 0 130 1097 -/+ buffers/cache: 1545 2406 Swap: 2055 0 2055 linux-8v2i:~ # free -m total used free shared buffers cached Mem: 3952 2773 178 0 130 1097 -/+ buffers/cache: 1545 2406 Swap: 2055 0 2055
total:总物理内存 used:已使用内存 free:完全未被使用的内存 shared:应用程序共享内存 buffers:缓存,主要用于目录方面,inode值等 cached:缓存,用于已打开的文件 -buffers/cache:应用程序使用的内存大小,used减去缓存值 +buffers/cache:所有可供应用程序使用的内存大小,free加上缓存值
total = used + free -buffers/cache=used-buffers-cached,这个是应用程序真实使用的内存大小 +buffers/cache=free+buffers+cached,这个是服务器真实还可利用的内存大小
大家都知道,Linux服务器为了提高效率,会提前申请内存,即使这些内存没有被具体应用使用,Linux也会提前申请这些内存,然后利用这些内存做缓存用,即将刚打开的文件系统存入cache中,这样对应的服务器free值会越来越少,buffers和cached会越来越大,因此给大家表象就是内存越来越少了,大家就紧张了;其实,大家完全不用紧张,Linux服务器在发现内存不足时,会自动清理cached区域,释放内存,然后继续增大cache,free继续减少。因此,那样手动降低内存使用率的方法,其实就是图一时之快,呵呵。 |
可用脚本,定时判断处理。
###清理内存缓存
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 2000 ]
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
fi
#Writing to this file causes the kernel to drop clean caches,dentries and inodes from memory, #causing that memory to becomefree.
#
#To free pagecache, use echo 1 > /proc/sys/vm/drop_caches;
#
#to free dentries and inodes, use echo 2 > /proc/sys/vm/drop_caches;
#
#to free pagecache, dentries and inodes, use echo 3 >/proc/sys/vm/drop_caches.#
#
#Because this is a non-destructive operation and dirty objects are not freeable, the user should #run sync first.
转载于:https://blog.51cto.com/bensonzbs/1228426