linux如何释放cache

by admin on 2019年9月6日

linux如何释放cache

用free命令查看内存时,发现cached占用量过大。
释放脚本如下:

手动释放cache
# echo 3 > /proc/sys/vm/drop_caches

linux下释放内存的cache

搬运工
http://serverfault.com/questions/597115/why-drop-caches-in-linux
http://www.linuxatemyram.com/

 

#!/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

 

 

[[email protected]
~]# free -m

将脚本保存到 /root/systemtools/freemem.sh
将脚本添加到crond任务,定时执行:

虽然内存cache对文件系统有加速,但是cache太多的文件会感觉内存不太够用,甚至系统被逼使用swap,导致系统响应变的巨慢.

             total       used       free     shared    buffers    
cached

# echo "*/30 * * * * root /root/systemtools/freemem.sh" >> /etc/crontab

有啥办法可以让它变快呢?

Mem:          2009       1999          9          0       1308      
 437

sysctl -w vm.drop_caches = 3

-/+ buffers/cache:        253       1756

Swap:         5951          0       5951

echo 3 > /proc/sys/vm/drop_caches

 

就可以让它自动的释放不用的cache了.

[[email protected]
~]# cat /proc/sys/vm/drop_caches 

相让它开机启动自动设置,就编辑/etc/sysctl.conf把vm.drop_caches =
3放进去吧.

0

下面是官方说明:

[[email protected]
~]# echo 3 > /proc/sys/vm/drop_caches 

  /proc/sys/vm/drop_caches (since Linux 2.6.16)

 

  Writing  to  this  file  causes the kernel to drop clean caches,

[[email protected]
~]# cat /proc/sys/vm/drop_caches 

  dentries and inodes from memory, causing that memory  to  become
free.

3

  To  free  pagecache,  use  echo 1 > /proc/sys/vm/drop_caches; 

[[email protected]
~]# sync

       to free dentries and inodes, use echo 2 >
/proc/sys/vm/drop_caches;

[[email protected]
~]# free -m

  to   free   pagecache,   dentries  and  inodes,  use  echo  3
>/proc/sys/vm/drop_caches.

             total       used       free     shared    buffers    
cached

用后感:

Mem:          2009        463       1545          0        176        
49

试了一下,以前只有100多M的自由空间,甚至swap还被用了200多M,现在剩余空间多大,你自己看看…空闲内存接近1.8G

-/+ buffers/cache:        236       1772

系统变得非常流畅.娃哈哈

Swap:         5951          0       5951

 

 

[[email protected]
~]# free

有关/proc/sys/vm/drop_caches的用法在下面进行了说明

             total       used       free     shared    buffers    
cached

 

Mem:       3913564    2119816    1793748          0       7168    
318448

 

-/+ buffers/cache:    1794200    2119364

/proc/sys/vm/drop_caches (since Linux 2.6.16)

Swap:      4000148          0    4000148

Writing  to  this  file  causes the kernel to drop clean caches,

[[email protected]
~]#

dentries and inodes from memory, causing that memory  to  become

 

free.

虽然内存cache对文件系统有加速,但是cache太多的文件会感觉内存不太够用,甚至系统被逼使用swap,导致系统响应变的巨慢…

 

 

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(8) first.

 

 

[[email protected]
~]# free -m total used free shared buffers cached Mem: 2009 1999 9 0
1308 437 -/+ buffers/cache: 253 1756 Swap: 5951 0 5951
[[email protected]
~]# cat…

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图