Page 1 of 6 123 ... LastLast
Results 1 to 10 of 53

Thread: HOWTO: Clear filesystem memory cache

  1. #1

    HOWTO: Clear filesystem memory cache

    I have read several forum posts where people have asked "How do I clear/free/dump/purge the memory cache?" A variation of the same answer is almost always given "You don't want to do that ... the Linux Kernel is smarter than you ... Don't try it ... Use dd to create a big file, then delete it ... etc ..." I never could find an answer that just explained how to do it, so I kept searching elsewhere, and here it is.

    NOTE: This may cause you to lose data, it may make your system slower, it may kill your cat. Do this at your own risk, and don't be surprised if it doesn't work out the way you wanted it to.

    With the CYA out of the way, here is how to free up as much memory as possible by dumping the cache. Execute as root, or with sudo:

    Code:
    # sync
    # echo 3 > /proc/sys/vm/drop_caches
    That's it. Not much to see here. The first command writes any cache data that hasn't been written to the disk out to the disk. The second command tells the kernel to drop what's cached. Not much to it. This invalidates the write cache as well as the read cache, which is why we have the sync command first. Supposedly, it is possible to have some cached write data never make it to disk, so use it with caution, and NEVER do it on a production server. You could ... but why take the risk?

    As long as you are running a post 2.6.16 kernel, those commands will work. I tested it on Feisty and Gutsy, and it worked perfect.

    I got this information from AP Lawrence.

    Enjoy!

  2. #2
    Join Date
    Oct 2006
    Beans
    124
    Distro
    The Feisty Fawn Testing

    Re: HOWTO: Clear filesystem memory cache

    "This invalidates the write cache as well as the read cache, which is why we have the sync command first."
    According to the documentation that you sited, this is not true.

    From the documentation cited in the cited documentation, "this is a non-destructive operation, and dirty objects are not freeable"

    So, using the drop_caches command is designed to be safe, and running sync just ensures that ALL the cache is clear. If you don't run sync, and unwritten data will stay in the cache.

    I hope its clear that I'm just getting this from the websites cited, and I can't personally verify this in anyway.

  3. #3
    Join Date
    Oct 2006
    Beans
    124
    Distro
    The Feisty Fawn Testing

    Re: HOWTO: Clear filesystem memory cache

    well, I tried it, nothing seems to have happened really, except now ubuntu is using less ram

    I had to ask to get a command that could be run with sudo, so I'm posting it here for reference:
    sudo echo 3 | sudo tee /proc/sys/vm/drop_caches

  4. #4
    Join Date
    Nov 2006
    Location
    There and back again
    Beans
    1,097

    Re: HOWTO: Clear filesystem memory cache

    Cool tip. I had to shutdown X server for it to really be effective. For GNOME the GNOME display manager will need to be disabled. I'm using Gentoo and the boot system is different for Ubuntu (Ubuntu uses RC, i believe?) but the xdm daemon needs to be stopped:

    Code:
    sudo /etc/init.d/xdm stop
    ctrl-alt-del.

  5. #5
    Join Date
    Jul 2005
    Location
    KRK, Poland
    Beans
    179
    Distro
    Xubuntu

    Re: HOWTO: Clear filesystem memory cache

    Hi!

    Thanks jmarler. This is exacly what I was looking for

    copter :]

  6. #6
    Join Date
    Jun 2008
    Location
    Belgrade, Serbia
    Beans
    10
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: HOWTO: Clear filesystem memory cache

    Thanks!
    I have seen the other treads, and find this one very useful!

  7. #7
    Join Date
    Jun 2008
    Beans
    3

    Re: HOWTO: Clear filesystem memory cache

    Thank you for this. I need to clear the cached memory due to VBOX hogging cached memory, if I open/close multiple virtual machines, the cache does not clear, and once it hits 100%, when I try and start another my system crashes.

  8. #8
    Join Date
    Sep 2009
    Beans
    4

    Re: HOWTO: Clear filesystem memory cache

    Just wanted to add a note to this. I have the system monitor on my panel, with memory enabled, and as soon as I typed the 2nd command the cached meter dropped instantly. Works great.

  9. #9
    Join Date
    Jun 2009
    Location
    Dublin, IRL
    Beans
    130
    Distro
    Ubuntu 10.04 Lucid Lynx

    Thumbs down Re: HOWTO: Clear filesystem memory cache

    Sweet!
    Works on Karmic. I use it after shutting down VBox XP machine. I must use sudo -s and then type the second command. Using only sudo doesn't work.

  10. #10
    Join Date
    May 2010
    Beans
    1
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: HOWTO: Clear filesystem memory cache

    Hi guys! This doesn't seem to work in 10.04. I get
    Code:
    bash: /proc/sys/vm/drop_caches: Permission denied
    I am not well versed in the terminal and have only been using linux for a couple of months now. I am using PAE with 6 gb of ram. Hope you guys can help. Oh, and the system monitor shows full memory cache use.

Page 1 of 6 123 ... LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •