Scott Klarr Jr
Linux: How to clear the cache from memory
Linux has a supposedly good memory management feature that will use up any "extra" RAM you have to cache stuff. This section of the memory being used is SUPPOSED to be freely available to be taken over by any other process that actually needs it, but unfortunately my Linux (three distros now, Mandriva 32 bit, and Mandriva 64 bit, and Opensuse 11 64 bit) thinks that cache memory is too important to move over for anything else that actually needs it.
I have 6 GB RAM in my computer. Whenever there is no cache being stored in the memory (i.e. when I first boot the computer), everything runs great. But as soon as it fills up with cache, my computer starts feeling like a 700mhz P2 running windows 98 stuffed full of malware. It's terrible..
Up until just now, I have been forced to restart every time this happens because I simply cannot get any work done while in this state of retardation. I can close every single program I'm running - and even then, simply right clicking would require some extended thinking before loading the context menu. Ridiculous.
Luckily, I found a way to clear out the cache being used. Simply run the following command as root and the cache will be cleared out.
sync; echo 3 > /proc/sys/vm/drop_caches
Thank you ariel for posting in the comments below about including the sync command before dropping caches.
Last 5 Linkbacks
- Feb 04, 2010vonsed.wordpress.com
- Jan 18, 2010forums.cpanel.net
- Dec 01, 2009www.zimbra.com
- Sep 01, 2009mymind.ir
- Apr 05, 2009gigatype.com



ariel Feb 29, 2008
Hello Scott, i found that you should run "sync" before writing to drop_cache.
Source: http://www.linuxinsight.com/proc_sys_vm_drop_caches.html
yiwu china Aug 27, 2010
yes,i agree with you.
Nat Feb 29, 2008
Check 'swapiness'
B K Apr 06, 2008
"Linux: How to clear the cache from memory".
U have 6 GB RAM?? then the matter is really terrible. Even the ISP servers hosted on Redhat Enterprise Linux rock on 2GB. Go for optimization of code basics. Gerhard Mourani's "securing and opimizing redhat linux edition" is a good ebook.
James Sep 14, 2008
You rock. This just saved me a reboot. The last of which took the machine down for a day because of the stupid journalized drive.
Thanks
Piotr Dec 15, 2008
THANKS
I have 1,5 GB and I thought about buying more.
IT really helps!!
Dawnet Jan 02, 2009
AWESOME!
I run a hosting company and I thought this problem was a memory leak because I can restart my box and everything runs fine but when it fills up I have this issue! I created a cron job to do this hourly to keep the server in tip top shape!
Thanks!
FergatROn Jan 05, 2009
Hey Scott,
Simply beautiful! I ran the command and watched in amazement as my memory bar dropped down! Thanks so much for the article and thanks Ariel for the "sync" addition! I don't know what the command does, but it works like a charm! ;)
Mandeep Jan 08, 2009
Hi,
Thanks for this help.
Is there a way to clean out the dirty memory?
Rick Hall Jan 14, 2009
Thanks for posting this command. It worked like a charm! :)
Casey Tinsley Feb 18, 2009
Awesome! Thanks :D I just installed Slackware 12.2 running XFS on all drives and have noticed a huge speed increase... But now my ram cache does tend to fill up rather quickly. This fixed the problem in mere seconds!
Thanks again :)
Romy Maxwell Mar 11, 2009
It seems many of you don't have any actual understanding of memory management, so I'd like to clear a few things up.
Memory that is cached is, in fact, available upon demand. However, the Linux kernel may decide to swap unused pages of a running process out to disk rather than reclaim cached memory, because page cache is typically a more valuable & efficient use of free memory than pages that a process hasn't accessed in a long while. And before you get all jittery, let me clarify that by 'unused pages' I don't mean your entire copy of Open Office running in the background -- I mean, for example, data that a process may have read upon starting but will NEVER access again during its lifetime. It's wasted space.
As Nat tersely pointed out, the /proc/sys/vm/swappiness file will allow you to tune just how aggressively the Linux kernel will choose 'unused memory' over page cache. The default is typically 60, and it is a percentage value. So, if you drop that number down to say, 20, you should have much less swapping going on, and much more reuse of your page cache.
On that note... for most applications, regularly clearing the cache is like chopping off both arms to cure a pinky infection. The cache exists for a reason -- it eliminates TONS of disk accesses. If you clear the cache, you will always have to load everything from disk, defeating its purpose and effectively wasting that memory.
The likely problem is that you have something running in the background, maybe an indexing service, that is filling up your cache and causing disk IO. Chances are, if you reduce your swappiness, or locate the culprit(s), or both, you won't have anymore problems.
Daniel Apr 05, 2009
Hey, thanks for the fix! I posted something on my blog linking back to here about it.
( http://gigatype.com/2009/04/linux-memory-management/)
As I said in my post, if you put this into a simple shell script, you can stick it into /etc/cron.hourly to keep the system's Cache clean automatically. You could, of course, put it in another Cron time, like Daily.
mostafa Sep 01, 2009
Very Good
Thanks
Macgyver Sep 07, 2009
EXCELLENT! EXCELLENT! EXCELLENT!
You guys saved be downtime!
Tushar Vedak Nov 03, 2009
It's really helpful command,
Thaks You:)
sudipt Nov 05, 2009
proc/sys/vm/drop_caches file is missing...how can i do this
Leon Bollerup Dec 14, 2009
Thanx, this helped alot.. thank you so much!
Brett Mahar Dec 25, 2009
I wrote a script to do this, so no need to type commands each time:
=======================
#!/bin/bash
clear
sync
su -c "echo 3 > /proc/sys/vm/drop_caches"
exit
=====================
Put the above (betwen the lines) code in a text file (save to your desktop).
On Ubuntu, right click document, and tick "allowing executing" under properties > permissions. Whenever you want to run, double click and then select "run in terminal" at dialog box. You will be promoted for root password. Other distros you might have to play around a bit to see how to run, or run from bash command line.
Then your computer will be free to frolic on the wide open plains of its empty RAM!
Brett.
Josh Jan 22, 2010
Excellent, thanks for the tip, it worked very well.
Josh Jan 22, 2010
Scott, it would be helpful if you could mention any disadvantages of using this method in the article, and also mention the script mentioned by Brett Mahar, very useful stuff
Ryan Mar 02, 2010
Nat at the top dropped a hint at 'swapiness'. This is new to me, so I wont comment on it, instead leave it to you to read about it.
http://www.linuxhowtos.org/System/Linux%20Memory%20Management.htm
Poueck Mar 03, 2010
Nobody read the excellent post of Romy Maxwell ??
Flushing the cache, just flush the cache. It does not increase the available memory for processes. The cache is already available memory for processes if they need it.
Regards
Johny Mar 13, 2010
very good, thanks!
Cars Reviews Apr 11, 2010
Hi thanks for your great share... I've try the command but I got this message:
bash: /proc/sys/vm/drop_caches: Permission denied
Can you please help me to clear this matter.
Wiating for your fast support
Thanks
kristopher Apr 13, 2010
You'll need to be root to execute this, that's why you're receiving the permission denied message.
Swapping to root shouldn't be an issue. However, remember that in Ubuntu "su root" doesn't work. You'll need to execute a "sudo su" and enter in your sudo password to swap to root.
After that, you're good to go.
Dan May 20, 2010
But this is since Linux 2.6.16 .... what about 2.6.9-55? I can´t use drop_caches in this version, any suggestion?
elpinor May 31, 2010
It was a big surprise to me when i googled â how to delete cash on linuxâ and quite accidentally i found your site. It took to me much more than 22 years to be an atheist and linux fun. Bravo for your site .
Elpinor from Athens Greece
Professor of Mathematics
Imran khan Jul 15, 2010
Thanks you so much scott for this great Article and very handy command
Also I would like to Thanks to Romy Maxwell to clarify things in details. I help a lot to understand what is happening? and why it is happeing? and what we should do?
Thanks a lot, Keep it up.Cheers
higkoo Aug 24, 2010
When I `echo 3 > /proc/sys/vm/drop_caches`,but when It will Change to 0 ?
And why the default is 0 ?