How to Flush Memory Cache on Linux Server
Some times we found a low memory of Linux systems running a while. The reason is that Linux uses so much memory for disk cache is because the RAM is wasted, if it isn’t used. Cache is used to keep data to use frequently by operating system. Reading data from cache if 1000’s time faster than reading data from hard drive.
Its good for os to get data from cache in memory. But if any data not found in the cache, it reads from hard disk. So its no problem to flush cache memory. This article have details about how to Flush Memory Cache on Linux Server.
Empty Linux Buffer Cache:
There are three options available to flush cache of linux memeory. Use one of below as per your requirements.
1. To free pagecache, dentries and inodes in cache memory
# sync; echo 3 > /proc/sys/vm/drop_caches
2. To free dentries and inodes use following command
# sync; echo 2 > /proc/sys/vm/drop_caches
3. To free pagecache only use following command
# sync; echo 1 > /proc/sys/vm/drop_caches
Setup Cron to Flush Cache Regularly
Its a good idea to schedule following in crontab to automatically flushin cache on regular interval.
# crontab -e 0 * * * * sync; echo 3 > /proc/sys/vm/drop_caches
The above cron will execute on every hour and flushes the cached memory on system.
Find Cache Memory uses in Linux
Use free command to find out cache memory uses by Linux system. Output of free command is like below
# free -m
total used free shared buffers cached Mem: 992 406 586 0 155 134 -/+ buffers/cache: 116 876 Swap: 2015 0 2015
Last column is showing cached memory ( 134 MB) by system. -m option is used for showing memory details in MB’s.