Ubuntu Forums ubuntu.com - launchpad.net - ubuntu help  

Go Back   Ubuntu Forums > The Ubuntu Forum Community > Other Community Discussions > Tutorials & Tips
Register Reset Password Forum Help Forum Council Search Today's Posts Mark Forums Read

Tutorials & Tips
The place to find Ubuntu related Tips & Tricks.

 
Thread Tools Display Modes
Old October 24th, 2007   #1
jmarler
First Cup of Ubuntu
 
Join Date: Oct 2007
Beans: 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!
jmarler is offline   Reply With Quote
Old December 3rd, 2007   #2
danbh
Just Give Me the Beans!
 
Join Date: Oct 2006
Beans: 74
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.
danbh is offline   Reply With Quote
Old December 3rd, 2007   #3
danbh
Just Give Me the Beans!
 
Join Date: Oct 2006
Beans: 74
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
danbh is offline   Reply With Quote
Old February 7th, 2008   #4
Gen2ly
Dark Roasted Ubuntu
 
Gen2ly's Avatar
 
Join Date: Nov 2006
Location: There and back again
Beans: 1,061
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.
Gen2ly is offline   Reply With Quote
Old February 8th, 2008   #5
Copter
Gee! These Aren't Roasted!
 
Copter's Avatar
 
Join Date: Jul 2005
Location: CZW/KRK, Poland
Beans: 184
Xubuntu 7.10 Gutsy Gibbon
Re: HOWTO: Clear filesystem memory cache

Hi!

Thanks jmarler. This is exacly what I was looking for

copter :]
__________________
"Computers are a lot like air conditioners - they work great until you open windows."
Linux user 394528 && Ubuntu user 6836
Copter is offline   Reply With Quote
Old August 18th, 2008   #6
cika.mancic
First Cup of Ubuntu
 
cika.mancic's Avatar
 
Join Date: Jun 2008
Location: Belgrade, Serbia
Beans: 10
Ubuntu 8.04 Hardy Heron
Send a message via MSN to cika.mancic
Re: HOWTO: Clear filesystem memory cache

Thanks!
I have seen the other treads, and find this one very useful!
cika.mancic is offline   Reply With Quote
Old August 10th, 2009   #7
bzz_
First Cup of Ubuntu
 
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.
bzz_ is offline   Reply With Quote
Old September 19th, 2009   #8
Inifekt
First Cup of Ubuntu
 
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.
Inifekt is offline   Reply With Quote

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 09:21 PM.


vBulletin ©2000 - 2010, Jelsoft Enterprises Ltd. Ubuntu Logo, Ubuntu and Canonical © Canonical Ltd. Tango Icons © Tango Desktop Project. bilberry