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
 

16 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
  12. (its a bit tiring to paste and enter so much) :P It works a bit for me... using free -m I found it only reduces a small amount, but not all for me. I am using linux mint, and had no problems with the "sysctl -w vm.drop_caches = 0".
    However I think I need more; my computer goes very slow, It has since I had to remove windows xp (sp1) after a failed (forced) service pack upgrade. what I want to know is, without buying ram, is there some way to return it? or is there a better way to make this computer faster?

    (any help would be much appreciated, and would be likely to work; mint was built off of ubuntu...)

    ReplyDelete
    Replies
    1. How much RAM do you have, you could try a different distribution which is not at all memory hungry, a good one should be arch linux which is not easy to configure. The best one should be debian!!!

      debian is tough to configure/install for newbies, but you could try linux mint debian edition which is called lmde, without doing much, this should make your system faster.

      It is same like android, this RAM recovery thing, you/me will think there needs to be something done to retrieve the RAM, but you know there is nothing needs to be done. Linux kernel is super efficient in reclaiming RAM when wanted. Till 2 weeks back I was using some task killer in android thinking that should reclaim memory, but you know I was surprised to know nothing needs to be done in android for reclaiming memory (some may differ), so is linux(infact android is linux as it uses a linux kernel)

      If you feel RAM is not enough, increase swap space.

      I strongly recommend you linux mint debian edition, which should surprise you

      Other things you can do to reduce RAM usage is follow windows route. Windows disables fancy graphics and effects when there is less RAM or ineffective GPU, you can as well live without fancy effects by going with XFCE or LXDE desktops instead of gnome

      Though, I can tell you you must be for a surprise when you hit lmde (linux mint debian edition)

      Delete