You can write to /proc/sys/vm/drop_caches
file to instruct kernel to drop clean caches, as well as reclaimable slab objects like dentries and inodes. Once dropped, their memory becomes free.
This is not recommended to clear memory cache on Linux systems, but it is safe. But clearing cache may cause performance issue with system. Since it discards cached objects from memory, it may cost a significant amount of I/O and CPU to recreate the dropped objects.
This tutorial will help you to clear memory cache on Linux/Unix system via command line.
How to Clear Cache in Linux System
There are three options available to clear cache in Linux system memory. Use one of below as per your requirements.
- Clear PageCache, dentries and inodes in cache memory
sync; echo 3 | sudo tee /proc/sys/vm/drop_caches
- Clear dentries and inodes only in cache memory
sync; echo 2 | sudo tee /proc/sys/vm/drop_caches
- Clear pagecache only in cache memory
sync; echo 31 | sudo tee /proc/sys/vm/drop_caches
Here the sync
command is used to increase the number of objects freed by the drop cache. Using this a user can claim more memory by clearing more dirty objects on the system.
How to Schedule Clear Memory Cache
If you have to clear buffer cache regularly, use the cronjob do it. Schedule the following in system crontab to automatically flush cache memory on a regular interval.
Open a terminal and execute ‘crontab -e’ command to edit crontab:
crontab -e
Append below entry to the file:
0 10 * * * sync; echo 3 | sudo tee /proc/sys/vm/drop_caches
The above cron will execute on every hour and flushes the memory cache on your system.
How to find Cache Memory in Linux
Use free command to find out cache memory uses by Linux system. Output of free command is like below
free -m
Output:
total used free shared buffers cached Mem: 16050 15908 142 0 12012953 -/+ buffers/cache: 834 15216 Swap: 0 0 0
Here the last column is showing cached memory (12953 MB) on Linux system. The -m option is used to show output MB’s.
Very good information, thank you
Thanks for the tip, it was helpful.
for who is having problems about permissions, you should do/try
sync; echo 3 | sudo tee /proc/sys/vm/drop_caches
the article would be more complete if integrated with some indications on how to check the processes that “contributed” to produced that cached dimension
It gives me
-bash: /proc/sys/vm/drop_caches: Permission denied
Which permission i need to provide, i am running it with root user.
It is a _bad_ idea to flush caches.
I think this “crontab -e” need to be in “root” permission, right? 0.o
This command i have followed but ram cache is not clear. please help you
Hi Team,
I have scheduled the below job everyday night 23:30 in Production.
Cache is getting reduced and immediately after 30mts, again its coming to 31 GB where my overall
RAM memory is 36 GB which is divided into 31 GB + 5 GB of swap.
30 23 * * * sync; echo 3 > /proc/sys/vm/drop_caches
Is there any way, we can able to identify this abnormality of memory growth as its a production server.
Have you tried checking for inodes consumption by certain processes?? , are you running a webserver?? give me more details
And remember clearing out your buffer and cache comes with consequences, especially if you’re running a webserver. It’s not a suggested action if you want to free up memory on production servers. I’ll suggest you look into migrating or cleaning out some data from the /var and /tmp directories rather than playing around with Cache and Buffer. As well try reducing the rotational frequency of logs being archived or stored into the /VAR directory, you can do so by editing some of the CRON config files.
Yes, tomcat server is running on that machine. And most of the memory is consumed by the buffer/cache. What could be the reason and how do we handle this situation in order to come out of this kind of problem.
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!