您的位置:首页 > 运维架构 > Linux

Linux 释放Cache

2020-04-19 11:26 796 查看

  有时候生产会发现,运行一段时间swap就会飙升,而且不下来,其实内存这个东西,怎么说够用不够用呢,看下swap就行,如果这个swap长时间动态平衡之后又增加,那么估计是有问题。


有关/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,
dentries and inodes from memory, causing that memory to become free.
 
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


这个定时清理内存的一加,有问题你的开发也发现不了 盒盒~~
当发生内存不足、应用获取不到可用内存、OOM错误等问题时,还是更应该去分析应用方面的原因,如用户量太大导致内存不足、发生应用内存溢出等情况,否则,清空buffer,强制腾出free的大小,可能只是把问题给暂时屏蔽了

想想一个情况,当你已经预定脚本在每天下午2点来清除内存缓存。那么其时该脚本会执行并刷新你的内存缓存。在某一天由于某些原因,可能您的网站的在线用户会超过预期地从你的服务器请求资源。
而在这时,按计划调度的脚本运行了,并清除了缓存中的一切。当所有的用户都从磁盘读取数据时,这将导致服务器崩溃并损坏数据库。因此,清除缓存仅在必要时并且在你的预料之中

#!/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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: