Many times systems faced low memory issues 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 the operating system. Reading data from cache if 1000’s time faster than reading data from hard drive.
It’s good for the os to get data from the cache in memory. But if any data not found in the cache, it reads from hard disk. So it’s no problem to flush cache memory. This article has details about how to Flush Memory Cache on Linux Server.
Clear Linux Memory Buffer Cache
There are three options available to flush cache of Linux memory. Use one of below as per your requirements.
- Free pagecache, dentries and inodes in cache memory
sync; echo 3 > /proc/sys/vm/drop_caches - Free dentries and inodes use following command
sync; echo 2 > /proc/sys/vm/drop_caches - Free pagecache only use following command
sync; echo 1 > /proc/sys/vm/drop_caches
Schedule Cron to Flush Cache Regularly
It’s a good idea to schedule following in crontab to automatically flush cache on the regular interval. Use ‘crontab -e’ command to edit cron on your system.
crontab -l 0 * * * * sync; echo 3 > /proc/sys/vm/drop_caches
The above cron will execute on every hour and flushes the memory cache on your 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
Sample Output
total used free shared buffers cached Mem: 16050 15908 142 0 12014953 -/+ buffers/cache: 834 15216 Swap: 0 0 0
Last column is showing cached memory ( 14953 MB) by system. -m option is used for showing memory details in MB’s.
nice
Thanks! Clear and precise.
hi
wow such a wonderful article so thanks for sharing
very nice article. thanks for share it with us
Stated clearly, very helpful.
How to flush DNS Cache,
getting 2 errors 403 permission denied LAMP stack
SFTP SSH for wordpress incorrect keys for the user
your help appreaciated
I am getting the error that why my DNS server consume a lot of memory (run by CentOS 6.6). When this issue happen, the PCs which are assigned DNS IP of this server DNS, they can not browse internet? How can I fix this?
My memory usage is getting iuncreaseds on Application server as well as DB server can I setup cron to clear the cache every 5 hours?
WIl it increase performance?
Yes, it will DECREASE performance. Please refer to the kernel documentation at
https://www.kernel.org/doc/Documentation/sysctl/vm.txt (search for drop_cache)
In my Centos server the cache memory keeps on increasing. If i run cronjob everyday, is it make any problem to the server RAM.
It will make any performance issues.
The advice of flushing cache is totally non-sense and only for debugging-purpose. Please refer to the Kernel documentation in
https://www.kernel.org/doc/Documentation/sysctl/vm.txt
If i run this command also. still it holds cache memory. Do u have any solution or idea to clear the cache memory.?
[[email protected] ~]# free -g && sync; echo 3 > /proc/sys/vm/drop_caches && free -g
total used free shared buffers cached
Mem: 141 123 18 17 0 17
-/+ buffers/cache: 105 35
Swap: 19 0 19
total used free shared buffers cached
Mem: 141 123 18 17 0 17
-/+ buffers/cache: 105 35
Swap: 19 0 19
[[email protected] ~]#
The system will not force active programs to disk in preference of keeping non-essential stuff in cache. It will drop the least recently used items from cache to make room for new programs rather than more your program to swap. It *will* move the inactive data for programs that are currently ‘running’ (i.e. in the wait queue) to swap if it has to. If your system is doing this it’s b/c whatever processes you are currently running are chewing up lots of RAM. Manually flushing your cache won’t do a thing. And this is LINUX not windows, Linux will intelligently use your RAM to improve your performance rather than leaving it idle. Forcing flushes is a silly thing to do unless you are running benchmarking programs multiple times.
Thanks! Clear and precise.
muchas gracias!