Linux server 'free -m' query result show 'cached' keeps increasing

created at 11-27-2021 views: 8


View cached is very large, not released, and increasing
The linux server free -m query finds that cached keeps increasing



Use the following command to view what is in the cached:

linux-fincore --pages=false --only-cached /*

cache size


After frequently accessing files under Linux, the physical memory will be used up quickly. When the program ends, the memory will not be released normally, but will always be used as a cache. At this time, it is necessary to perform the operation of releasing the memory (clearing the cache). . Under normal circumstances, Linux does not need to manually release memory frequently.

Periodically clean up the cache method (add the following script to the timing task to execute):

used=`free -m|awk 'NR==2'|awk '{print $3}'`
free=`free -m|awk 'NR==2'|awk '{print $4}'`
echo "=============================================================" >>/var/log/mem.log
echo "`date`" >>/var/log/mem.log
echo "Memory usage before [Use:${used}MB][Free:${free}MB]" >> /var/log/mem.log
if [ $free -le 4000 ];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
        used_ok=`free -m|awk 'NR==2'|awk '{print $3}'`
        free_ok=`free -m|awk 'NR==2'|awk '{print $4}'`
        echo "Memory usage after [Use:${used_ok}MB][Free:${free_ok}MB]" >>/var/log/mem.log
        echo "OK" >>/var/log/mem.log
        echo "Not required" >>/var/log/mem.log
exit 1

In addition, when the application runs stably on the system, the free value will remain at a stable value, although it may seem to be relatively small. When problems such as insufficient memory, applications fail to obtain available memory, OOM errors, etc., it is still better to analyze the application reasons.

created at:11-27-2021
edited at: 11-27-2021: