Home page of this blog

Monday, September 28, 2009

Clearing cache memory in linux using sysctl

In linux, when we copy big files, the file's content gets cached in RAM for fast access eventhough we might not need it later in RAM. We can forcefully clear the RAM cache using sysctl

sudo sync && sudo sysctl -w vm.drop_caches=3 && sudo sysctl -w vm.drop_caches=0

The above line is a combination of three commands
1. sync --> to ensure any pending cache is flushed onto disk
2. sysctl -w vm.drop_caches = 3 --> to clear all caches (pagecache and inode/dentry caches)
3. sysctl -w vm.drop_caches = 0 --> reset the drop_caches to no clearing

The above command clears all caches from RAM. Beware, this clearing cache works in Ubuntu fine, but in other distributions maynot work fine
Screenshot showing free RAM before clearing cache
 
Screenshot showing free RAM after clearing cache
 

14 comments:

  1. Thanx a lot.. it's very help full in my Squid Server.. before Squid Server using the memory, system take it for system's caching, and make Squid suffered, and restarting the system.. and i try to use your tips in cron hourly, hope this will help my problem.. Thanks again..

    ReplyDelete
  2. This works very good. Thanks for the help!

    ReplyDelete
  3. how to create cron; to make this command running every 30 minutes?plz help me friend...:)

    ReplyDelete
  4. Hi Helmi,

    These are the steps to create a crontab

    1. Become root as sysctl will execute only with root privileges
    sudo su
    2. edit/create a new cron entry using the following command
    crontab -e
    3. paste the following line in the nano editor
    */30 * * * * sync && /sbin/sysctl -w vm.drop_caches=3 && /sbin/sysctl -w vm.drop_caches=0

    4. save the file and exit and your cron will execute every 30 mins once and clear the cache

    5. to list your root cron entries, use sudo crontab -l and to remove cron entries use sudo crontab -e

    References
    I followed the following two articles to get this cron working

    http://adminschoice.com/crontab-quick-reference
    http://www.willmaster.com/library/web-development/using_cron.php

    ReplyDelete
  5. why to clear cache memory?

    If you copy a big file (4 GB file) your RAM will be cached aggressively by linux. This may lead to RAM starvation though you have really loads of RAM, though it may be cleared by kernel after sometime.

    Now you have two options, either to delete the copied file or unmount some filesystem where it is copied to free up RAM space.

    Or you can forcefully clear the cache and reclaim it for some other applications use

    ReplyDelete
  6. why did I refer those pages for crontab?

    Because, I don't know them before and what do you think wrong in refering them ?

    Or did you mean, why to use crontab ?

    crontab is a scheduler through which you can schedule tasks to happen between scheduled intervals

    ReplyDelete
  7. This is a terrible idea. There is no benefit to dropping the cache AFTER anything good has already been wiped out. Cache is FIFO so dropping it now is essentially a no-op.

    What you really want to do is stop the file copy from using the cache in the first place.
    Try these:

    http://enricozini.org/2010/tips/nocache/
    https://code.google.com/p/pagecache-mangagement/

    ReplyDelete
  8. I still feel the necessity for clearing the cache, who asked linux to first cache my file when there is requirement for more RAM. Isn't it ridiculous to go so much as creating another program like nocache to avoid caching or write program yourself.

    See windows, it does not unnecessarily cache and even after large copy operations, it remains stable

    And according to article it is saying it is based on swap. pagecache has nothing to do with swap, when pagecache overflows it is swapped which is very ugly

    Caching a copy/move operation by OS is a bad idea, eventhough it caches, let us say if there is an upperlimit for caching it will be fine.

    Why do you think people go for direct IO in databases, isn't it to avoid this stupid caching behavior

    ReplyDelete
  9. Hi Sankaran, I'm a very happy fan of your kernel compilation... howewer i have a question about this article:

    sudo sysctl -w vm.drop_caches=0 gives me an error (error: "Invalid argument" setting key "vm.drop_caches") while sudo sysctl -w vm.drop_caches=3 gives me not (1,2 value works fine too). Is there another way to reset the cache to no clearing instead of putting vm.drop_caches="0"? my kernel in your last 2.6.39 amd64 core2.

    Keep up the good work!

    ReplyDelete
  10. Yeah, I checked it, vm.drop_caches=0 does not work


    will update you, thanks for appreciation

    ReplyDelete
  11. know what, writing to drop_caches using

    sudo sysctl -w vm.drop_caches=3 immediately clears the pagecache, dentries and inodes

    and clears again only if you call sysctl -w again, there is no need to explicitly set it back to 0, this is just my observation

    ReplyDelete