Saturday, August 20, 2011

drop or clear cache in linux


Many times you may find the system is running out of memory. When checked you can see lots of memory is assigned to buffers and caches.Allocating lots of memory to buffers and caches is not necessary. If you are running mysql and oracle like softwares, they have their own buffers and caches. So mostly you can free or drop this buffers and caches. This post explains how to drop caches in Linux. Also the entry for sysctl.conf so that it will remember the action.

According to the linux documentations, the variable drop_caches defined as, Writing to this will cause the kernel to drop clean caches, dentries and inodes from memory, causing that memory to become free.

To free pagecache:
echo 1 > /proc/sys/vm/drop_caches
To free dentries and inodes:
echo 2 > /proc/sys/vm/drop_caches
To free pagecache, dentries and inodes:
echo 3 > /proc/sys/vm/drop_caches

As this is a non-destructive operation and dirty objects are not freeable, the user should run `sync' first.
So the command to drop all caches are,
#sync; echo 3 > /proc/sys/vm/drop_caches

Or you can specify this in /etc/sysctl.conf
#echo "vm.drop_caches = 3" >> /etc/sysctl.conf
Now reload sysctl.conf
#sysctl -p

1 comment:

  1. Not 100% sure about this, but your "Also the entry for sysctl.conf so that it will remember the action." sentence is not clear, or could be wrong, depending on what do you mean by it.
    Remember the action could mean that the kernel will disable caching of pages, dentries and inodes, but AFAIK it will only 'remember' to execute the command once after each boot.
    Some people recomend setting vm.drop_caches=3 in sysctl.conf in order for cache to be rebuilt with something more useful than stuff which got there during boot process.


Be nice. That's all.