linux系统管理--buffre/cache的释放

本篇文档主要讲述的是linux系统管理的 buffre/cache空间释放

[root@VM_0_8_centos ~]# free -h
              total        used        free      shared  buff/cache   available
Mem:           992M        170M         78M        388K        744M        640M
Swap:            0B          0B          0B

含义注释:
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

此处和大家说一下什么是cache

为了提高磁盘存取效率,Linux做了一些精心的设计,除了对dentry进行缓存(用于VFS,加速文件路径名到inode的转换),还采取了两种主要Cache方式:Buffer Cache和Page Cache。前者针对磁盘块的读写,后者针对文件inode的读写。这些Cache有效缩短了 I/O系统调用(比如read,write,getdents)的时间。

cached主要负责缓存文件使用, 日志文件过大造成cached区内存增大把内存占用完 .

Free中的buffer和cache:(它们都是占用内存):
buffer : 作为buffer cache的内存,是块设备的读写缓冲区
cache: 作为page cache的内存, 文件系统的cache
如果 cache 的值很大,说明cache住的文件数很多。

手动释放buffer/cache

释放方法有三种(系统默认值是0):

To free pagecache: echo 1 > /proc/sys/vm/drop_caches

To free dentries and inodes: echo 2 > /proc/sys/vm/drop_caches

To free pagecache, dentries and inodes: echo 3 > /proc/sys/vm/drop_caches

注意:在清空缓存前我们需要在linux系统中执行一下sync命令,将缓存中的未被写入磁盘的内容写到磁盘上

释放的方法

[root@VM_0_8_centos ~]# free -h
              total        used        free      shared  buff/cache   available
Mem:           992M        170M         78M        388K        744M        640M
Swap:            0B          0B          0B
[root@VM_0_8_centos ~]# sync
[root@VM_0_8_centos ~]# sync              
[root@VM_0_8_centos ~]# echo 3 >/proc/sys/vm/drop_caches 
[root@VM_0_8_centos ~]# free -h
              total        used        free      shared  buff/cache   available
Mem:           992M        169M        735M        388K         87M        702M
Swap:            0B          0B          0B

计划任务脚本

脚本copy过去直接执行就可以了

[root@VM_0_8_centos ~]# cat buffer.sh 
#!/bin/bash
#discription:The script is release buffer/cache
#Writer by mslinux mail : mslinux@163.com

[ -e /var/spool/cron/root ]
if [ $? -eq 0 ]
then
cat >> /opt/buffer.sh << EOF
#!/bin/bash
#discription:The scipt is release Buffer/cache
#writer by mslinux
sync
sync
sync
echo 3 > /proc/sys/vm/drop_caches
EOF
	echo "0 2 * * * /usr/bin/bash /opt/buffer.sh" >> /var/spool/cron/root
	if [ $? -eq 0 ]
	then
		systemctl restart crond.service
	fi
else
	echo " Not find cront list Please exit"
fi

[root@VM_0_8_centos ~]# bash buffer.sh 
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值