Linux: How to clear the cache from memory

Posted On Feb 28, 2008 at 10:17 pm

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.

This topic has the following tags:

Last 5 Linkbacks

Comments (30)

Romy Maxwell's Avatar

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's Avatar

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.

Post Comment

You are replying to cancel reply

Your email address will not be visible to the public

Avatars by Gravatar. Join Gravatar for free to have your avatar shown everywhere you post.