UnixServerAdmin

Server Administration & Management

How to 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. Here is process 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

February 28, 2012 Posted by | Tips & Tricks, Unix/Linux | , , | Leave a comment