Clear the cache in Linux efficiently and securely

1. Command to clean up the cache:

free -h/-m                               #查看目前内存使用情况
#也可筛选计算出剩余内存百分比,如下:
free -m | sed -n '2p' | awk '{print "used mem is "$3"M,total mem is "$2"M,used percent is "$3/$2*100"%"}'
 
echo 1 > /proc/sys/vm/drop_caches     #释放页缓存
 
echo 2 > /proc/sys/vm/drop_caches     #释放dentries和inodes缓存
 
echo 3 > /proc/sys/vm/drop_caches     #是释放 1  2 中说道的的所有缓存

2. Detailed explanation

When using free -h to view memory usage:
Insert picture description here

(1) The difference between Men and Swap

Column 1 Mem
physical memory usage

total used free shared buff / cache available
Total physical memory capacity Used memory capacity Free memory capacity Shared memory capacity Cache capacity Available memory capacity

It can be seen from the value in the figure that total≠used+free, but total≈used+available; and
free: is the actual unused physical memory capacity
available: is the memory capacity that the application considers available, available≈free+buff /cache, the key is the buff/cache part.

Column 2 Swap
swap area memory usage

total used free
Total swap partition capacity Swap partition usage capacity Free capacity of swap partition

(2) The difference between buffer and cache

1. Both the buffer and the cache are to solve the difference in the rate of the two devices for mutual access, to make the read and write performance of the disk I/O or the cpu more efficient, and to reduce the waiting time for communication between processes

2. Buffer: Buffer, used to transfer data between devices with unsynchronized storage speeds or devices with different priorities. Through buffers, the waiting time for inter-process communication can be reduced. When storage speeds are fast and storage speeds are slow When communicating, the fast storage device caches the data to the buffer first, and waits until the system uniformly writes the data on the buffer to the slow device. Commonly, the data in the memory is written to the disk. At this time, you can check the buffers

3. Cache: The cache area is used to restrict the read speed, but because of the speed difference between the storage devices, the data cannot be obtained immediately. At this time, the cache will cache part of the data in order to speed up. Common is the data communication between the CPU and the memory, because the speed of the CPU is much higher than the speed of the main memory, it takes a long time for the CPU to read data from the memory, and the Cache saves the data or Part of the data used in the cycle, then the data will be read faster in the Cache, which reduces the waiting time of the CPU and improves the performance of the system.

Three, one-click cleanup of cached scripts

#!/bin/bash
#Men分区内存总量 
mem_total=`free -m | awk 'NR==2' | awk '{print $2}'`

#Men分区当前剩余内存的大小 
mem_free=`free -m | awk 'NR==3' | awk '{print $4}'`

#Men分区当前已使用内存的大小 
mem_used=`free -m | grep Mem | awk '{print  $3}'`

if (($mem_used != 0)); then

#如果已被使用,则计算当前剩余free所占总量,表示为整数
mem_per=`free -m | awk '/Mem:/ {print int($3/($3+$4)*100)"%"}'`
mem_total=`free -m | awk '/Mem:/ {print int($3/($3+$4)*100)}'`
#设置监控日志监控内存使用情况
DATA="$(date -d "today" +"%Y-%m-%d-%H-%M") free percent is : $mem_per"
echo $DATA >> /var/log/mem_alarm.log

#当前剩余百分比与80%进行比较 ,超过80%进行缓存清理
if [ $mem_total -gt 80 ]; then
sync
echo 1 > /proc/sys/vm/drop_caches
echo 2 > /proc/sys/vm/drop_caches
echo 3 > /proc/sys/vm/drop_caches

else
echo "Don't have to clean up"

#将release memory OK !写入/var/log/文件夹的memstat_日期.log日志中
echo "--->release memory OK ! " >>/var/log/memstat_$(date +%Y%m%d_%H%M%S).log
fi
fi

Guess you like

Origin blog.csdn.net/Gengchenchen/article/details/113701832