嗯,关于缓存清理,小伙伴一定不陌生,个人以为通过修正drop_caches内核参数的形式来清理缓存,只能是在生产环境用于临时办理问题,实质还是要对运用进行剖析,看看是什么缘故原由导致的内存溢出等OOM问题,一样平常情形下,内存溢出可以通过交流分区来确定
当然,如果是由于业务量的缘故原由,业务高峰,或者须要算力等正常缘故原由,可以定期通过sync将缓存区没有写入数据写入磁盘,然后修正内核参数 drop_caches的值来清理缓存,或者通过新建交流分区调度swappiness交流分区频率来办理问题,如果虚机的话可以申请扩内存
系统内存查看

个中: Mem:实际的内存 | Swap: 交流分区 |
total 内存总数used 已经利用的内存数free 空闲的内存数shared 多个进程共享的内存总额buff/Cache 缓存的内存大小available 可用内存系统内存监控系统内存监控
vmstat: 是一个内存监控工具,后面的数字为刷新频率 | top 不多说
vmstat
列描述
free
空闲的物理内存的大小。
buff
Linux/Unix系统是用来存储,目录里面有什么内容,权限等的缓存.
cache
cache直接用来影象我们打开的文件,给文件做缓冲,我本机大概占用300多M(这里是Linux/Unix的聪明之处,把空闲的物理内存的一部分拿来做文件和目录的缓存,是为了提高 程序实行的性能,当程序利用内存时,buffer/cached会很快地被利用。)
修正drop_caches内核参数清理缓存我们先看看帮助文档:内核参数的帮助文档查看办法
┌──[root@liruilongs.github.io]-[/proc/sys/vm]└─$ man -K drop_caches--Man-- next: proc(5) [ view (return) | skip (Ctrl-D) | quit (Ctrl-C) ]
嗯,英文好的小伙伴可以看看
drop_cachesWriting to this will cause the kernel to drop clean caches, as well asreclaimable slab objects like dentries and inodes. Once dropped, theirmemory becomes free.To free pagecache:echo 1 > /proc/sys/vm/drop_cachesTo free reclaimable slab objects (includes dentries and inodes):echo 2 > /proc/sys/vm/drop_cachesTo free slab objects and pagecache:echo 3 > /proc/sys/vm/drop_cachesThis is a non-destructive operation and will not free any dirty objects.To increase the number of objects freed by this operation, the user may run`sync' prior to writing to /proc/sys/vm/drop_caches. This will minimize thenumber of dirty objects on the system and create more candidates to bedropped.This file is not a means to control the growth of the various kernel caches(inodes, dentries, pagecache, etc...) These objects are automaticallyreclaimed by the kernel when memory is needed elsewhere on the system.Use of this file can cause performance problems. Since it discards cachedobjects, it may cost a significant amount of I/O and CPU to recreate thedropped objects, especially if they were under heavy use. Because of this,use outside of a testing or debugging environment is not recommended.You may see informational messages in your kernel log when this file isused:cat (1234): drop_caches: 3These are informational only. They do not mean that anything is wrongwith your system. To disable them, echo 4 (bit 2) into drop_caches.
手动实行sync命令(描述:sync 命令运行 sync 子例程。如果必须停滞系统,则运行sync 命令以确保文件系统的完全性。sync 命令将所有未写的系统缓冲区写到磁盘中,包含已修正的系统 i-node、已延迟地块 I/O 和读写映射文件)
┌──(rootLiruilong)-[/mnt/c/Users/lenovo]└─# sync
对缓存进行清理
┌──(rootLiruilong)-[/mnt/c/Users/lenovo]└─# free -m total used free shared buff/cache availableMem: 12480 84 12355 0 40 12228Swap: 4096 0 4096┌──(rootLiruilong)-[/mnt/c/Users/lenovo]└─# echo 3 > /proc/sys/vm/drop_caches┌──(rootLiruilong)-[/mnt/c/Users/lenovo]└─# free -m total used free shared buff/cache availableMem: 12480 84 12372 0 23 12237Swap: 4096 0 4096
清理缓存
我们来看看文档
写入drop_caches将导致内核丢弃干净的缓存,以及可回收的slab工具,如dentry和inode。一旦低落,他们内存得到开释。详细的参数描述
To free pagecache:echo 1 > /proc/sys/vm/drop_cachesTo free reclaimable slab objects (includes dentries and inodes):echo 2 > /proc/sys/vm/drop_cachesTo free slab objects and pagecache:echo 3 > /proc/sys/vm/drop_caches
利用此文件可能会导致性能问题。由于它丢弃了缓存工具,它可能会花费大量的I/O和CPU来重新创建掉落的物体,特殊是大量利用时。由于这个缘故原由,不建议在测试或调试环境之外利用。
在内核的文档里也说不建议在测试或调试环境之外利用,重修一些须要的缓存还是会花费大量的I/O和CPU,同时这也不是一个必要操作,一些不用的缓存系统会自动的清理掉
一个SHELL末了在和小伙伴分享一个清理缓存的shell
#!/bin/bash#@File : clear.sh#@Time : 2021/12/27 23:52:20#@Author : Li Ruilong#@Version : 1.0#@Desc : 打消缓存的shell#@Contact : 1224965096@qq.commax=10000 # 缓存的最大值clearLog=/var/log/clearfreelogif [ ! -f $clearLog ];then touch $clearLogfifilesize=$(du -m $clearLog | awk '{ print $1 }')echo "filesize=$filesize"if [ $filesize -gt 300 ]; thenecho $clearLog > $clearLogficache=$( free -m | grep Mem | awk '{print $(NF-1)}')time=$(date)if [ $cache -gt $max ]then echo "$time cache=$cache flush cache start!" >> $clearLog sync echo 3 > /proc/sys/vm/drop_caches echo "$time FreeMemory Success!" >> $clearLogelse echo "$time cache=$cache Memory is normal" >> $clearLogfi