Clear memory caches on Linux

04 Mar 2010 at 00:00:00 - 9 comment(s)

You have a machine with a lot of RAM. After sometimes you notice that you have hardly any RAM left and that it starts to swap. Though if you look at your process using top for example you notice that none of your application are using all your memory. This is because Linux is caching stuff into memory and unfortunately when you need to use more memory rather than freeing some cache it will start to swap. So let's see how to clear the memory cache.

Just by using a really simple command:

sync; echo 3 > /proc/sys/vm/drop_caches

sync should be run because this is a non-destructive operation, and dirty objects are not freeable. So you run sync in order to make sure all cached objects are freed.

Doing echo 3 is clearing pagecache, dentries and inodes but you could choose echo 1 to free pagecache only or echo 2 to free dentries and inodes.


Notify me of follow up comments