How to Clear Memory Cache on Linux

How to Clear Memory Cache on Linux

By default, every Linux OS has an efficient memory management system used to clear the buffer cache periodically. You can manually free up the memory cache with the following simple command:

sk@sk:~$ sudo sh -c "sync; echo 3 > /proc/sys/vm/drop_caches"

However, if you want to force the Linux OS to do clearing memory cache on a particular interval, just add the command to cron job. Here, I show you how.

Open up your terminal and enter the following command to create a file called Say for example, I create file in my /home directory:

sk@sk:~$ sudo vi /home/

Add the following lines to file:

sudo sh -c "sync; echo 3 > /proc/sys/vm/drop_caches"

Save and exit the file. Now add this file to your crontab:

sk@sk:~$ sudo crontab -e

This command opens the current user cron file. If you want to set it to some other user, use the following command instead:

sk@sk:~$ sudo crontab -e -u username

Add the following line at the end:

0 * * * * /home/

Save and exit the file. Once you’re done, cron job will run this command every hour and will clear the system memory caches.

After testing with the above command, the sample results are shown in the below screen shots.

Before running the memory cache clearing script.

sk@sk: ~_012After running the memory cache clearing script.

sk@sk: ~_013As per the above outputs, the memory caches are reduced a bit.

  • Curtis

    If you’re going to use sudo anyway, I’m curious as to why you don’t just do something like:

    $ sync; sudo echo 3 > /proc/sys/vm/drop_caches

    Additionally, if you are going to add this to your root crontab (by doing “sudo crontab -e”), why are you going to have root run sudo? If it is in your root crontab, you don’t need to elevate permissions which are already top level.

    Besides that, I am unsure why you wouldn’t just add the command directly to the crontab itself. There is really no point in having cron call a one line script. At that point you are just making unnecessary subshells.

    Finally, I don’t think this is a smart thing to do at all. The kernel handles memory allocation just fine. Your buffers and cache exist so that when you use a program and exit out of it, it is marked as inactive so that it can save it in case you re-open that program again. If it runs out of room, it rids itself of the oldest inactive parts. So really, you are just making your computer more inefficient.

    There are reasons to clear the buffers and cache, but the reasons are few and far between, and certainly should not ever be recommended unless for one of those reasons.

    To anyone who may be reading this post: do not add this as a cronjob, it is pointless (and will potentially work against the efficiency of your computer).

  • littlenoodles

    Why would you want to manually clear memory caches? Aren’t they cleared automatically as needed on a ‘last used, first cleared’ basis? Is that process inefficient enough to slow the system down?

  • e8hffff call

    I find my memory on Kubuntu is constantly allocated and probably not utilized. When I run new programs, they seem to struggle when the memory is grabbed by other processes or disk-cache. I have 18GB of chip memory and the system is probably forcing programs to virtual memory (harddrive) when it’s not readily available.

    I’d like program to use what they need and the disk-chace to use less.

  • e8hffff call

    Example my current disk-cache is 14GB. yep I’m going to be moving 14GB files around or my processes doing the same…NOT. All while the free memory is a measly 160MB.

    It’s obvious Debian(Ubuntu/Kubuntu) isn’t tailored to current memory configuration and system usage. Otherwords memory is cheap to obtain over 8GB

  • David Brown

    Of course Linux will use all available memory for disk cache – why would it not do so? Would you rather it left memory wasted? Keeping disk data in cache speeds up disk access, so Linux keeps disk data on hand whenever it has the free memory space.

    Whenever a program needs memory, disk cache is dropped automatically to make space. Linux does it’s best to push out the least useful disk cache data (the algorithms can never be perfect without using a crystal ball, but they are pretty good).

    Live program data is only pushed into swap if the system’s memory is very tight. “Dead” program data (such as startup code, unused code, or memory leak areas) can get pushed into swap if the system thinks the memory is better used as disk cache.

    In short, the system is /far/ better at memory management than /you/ are. Let it do it’s job. It works well with memory ranging from a few MB to multiple TB.

  • David Brown

    You haven’t said anything about why it could be useful to clear your caches. That’s not very surprising, since there are almost no situations in which it really is a good idea. In particular, it is a terrible idea to do it regularly – it will pointlessly slow down your system.

    Far and away the biggest use of cache clearing is for benchmarking. If you want to know how fast your disk or filesystem is, you need to clear your caches before testing in order to get consistent results.

    Another situation is if you have just done a lot of disk work, and you know the data in the disk cache will not be needed again. An example of that would be if you have just finished a large backup. Even then, it is doubtful that it would be helpful overall.

    For special situations, you may be interested in pre-seeding your caches, by copying files to /dev/null in order to ensure they are in the cache for fastest possible access when you need to read them. You might want to clear your cache in advance to make sure the data you want has space. But I can’t think of much use of such pre-seeding, except perhaps for benchmarking (and you would probably be better off putting the data into a tmpfs filesytem rather than messing about with the caches).

    Finally, you might want to clear your caches as part of a very high security procedure – empty your caches, then fill your memory with non-secret data to ensure that no secrets are left around in ram.

    Oh, and you don’t need “sync” before clearing the caches. Linux is smart enough to write out dirty data to disk before removing it from the cache – it doesn’t have to be told something that blindingly obvious.

  • e8hffff call

    It would be better if disk-cache grew when needed rather then free-memory for applications being allocated desperately.

    Applications may look for free memory and configure themselves for scarcity due to disk-cache over grab, rather than getting the resources a program needs to run best.

    Like said it seems starting new programs are sluggish when the disk-cache is filling free memory.

  • David Brown

    Please stop trying to guess how you think the system works – you are getting it badly wrong. Use a little common sense here – do you think you are the only person who has ever started a program when the disk cache is filling your free memory? Do you think you have come across a situation that has not occurred to thousands of kernel developers, and tens of millions of other users?

    When programs need memory, Linux gives them memory. The program neither knows nor cares whether that memory was “free” or contained disk cache data. If they ask for more memory than the system can provide in ram after freeing up disk cache space, they will get space on swap – or an error return code. It is that simple.

    Of course, Linux is tunable in these things – there are ways to control disk caching, and how much or little it swaps to disk. But I am reluctant to tell you more, because I fear you will pick random bits of the information and make a mess of your system while trying to change things.

    So please trust me here – the people that made the kernel, and the people that tuned it and configured it for Kubuntu, know far more about this than you do. There are no issues with disk caches “stealing” resources from programs. There are no issues with programs starting slowly because of data in disk caches (the disk cache often leads to faster program startup – that’s one of the reasons for having it). The system is already tuned to work well on a range of desktops, including machines with 16 GB or your mythical 18 GB machine.

  • salam

    Hi , I won’t suggest doing this often as this could lead to disk corruption.

    And cache improve the performance of linux greatly by keeping frequently read disk sections in memory.

    I would suggest doing this if you find oom killer messages in dmesg.

  • e8hffff call

    I already know about sysctl

    Who say you are an authority of kernel memory management.

    In my case are you saying 14GB of disk-cache will get used. I’d prefer a much smaller scalable allocation, or so that more free memory is available the more memory you have, even a curve on the allocation formula (not being linear) I fathom disk-cacheis efficient if you have data that needs to be read again and again, but 14GB is ridiculous.

  • David Brown

    I do not claim to be an “authority” on kernel memory management (though I know far more than most people) – but it is apparent from your questions and criticisms that you don’t understand it. That’s fair enough, as it’s a rather specialist subject. I just want you to understand that you don’t know enough to making these kind of claims and complaints.

    Have you heard the phrase “law of diminishing returns”? Disk cache space follows such a law. What that means is that you get most benefit from the first parts of the cache, and less extra benefit as you add more memory. So in many cases, 14 GB used for disk cache is not going to be much faster than 2 GB disk cache. But it also means that you always get /some/ benefit from increasing the cache. 14 GB might not be much faster than 2 GB – but it is still a little bit faster. And those 14 GB are not being used for anything else. So putting disk data there costs nothing, and gives a little speed benefit. So why are you complaining?

    You seem to be convinced that using free memory for disk cache takes away memory from programs. I’ve told you that this is not the case. I’m not sure what more I can do to convince you, except perhaps point you here:

    You are far from the first Linux newbie to ask these questions – but you are unusually persistent in not believing the answers.

  • xsub

    David Brown is right, you are not.

    If you want ease the disk, and have ~18GB of RAM, disable swap.

    And stop spreading FUD.

  • Frédéric

    The way SK did allows all commands (sync, echo) _and_ the result of the echo command (write to the drop_caches file) to be executed as root. Doing like you propose only execute the echo command as root, so unless you are already logged in as root (or the user you are logged in with has enough privileges), the sync and the write to the drop_caches file will do nothing.

    For the the crontab, you are right, there’s no need for a sudo command in the script as it is installed in the crontab of root, _unless_ you want to be able to execute that script as a non root user: having sudo in it, you don’t have to remember that you need to prefix with sudo when you want to run it.

    For your remark about the uselessness of that entry in the crontab, I agree 100%.

  • Warrior

    The system will not force active programs to disk in preference of keeping non-essential stuff in cache. It will drop the least recently used items from cache to make room for new programs rather than more your program to swap. It *will* move the inactive data for programs that are currently ‘running’ (i.e. in the wait queue) to swap if it has to. If your system is doing this it’s b/c whatever processes you are currently running are chewing up lots of RAM. Manually flushing your cache won’t do a thing. And this is LINUX not windows, Linux will intelligently use your RAM to improve your performance rather than leaving it idle. Forcing flushes is a silly thing to do unless you are running benchmarking programs multiple times.

  • Warrior

    I don’t think this will help in the case of OOM messages. OOM messages means that active processes cannot allocate RAM. Linux will drop non-dirty disk buffer cache items from memory when the memory is required for an actively running program.

  • Domenico Viggiani

    Can this be useful for real IO tests on disks? Right?

    Clear cache before every test helps to get real results, without using files larger than RAM.

  • littlenoodles

    One thing that annoys me no end about disk cache though. It seems you can’t say ‘for this operation, I don’t want you to cache the data – just read it into my buffer please’.

    Maybe this is possible on Linux, but on AIX, it doesn’t seem to be. In any case, I find that certain operations – like a full filesystem backup – end up flushing useful stuff out of the cache in favor of stuff that’s guaranteed to be used exactly once. A special flag saying don’t cache stuff read by this program would help.

  • mangoo

    You may use the command manually when doing some kinds of benchmarking, but adding it to cron is the stupidest thing to do on your Linux system!

  • Kyle

    Well said. This can’t be stressed enough.

  • jp

    I like to game in linux and this trick is lovely for that purpose. The cache will just keep filling and filling, and the system is not noticeably slower after dumping the cache, but if the game gets a full memory error it will crash.

  • Phartat Misassa

    Thanks everyone, I’m running an old tower as a server, it only has 512MB of RAM and about 300 of that was cache, and only 5 MB free, so thats how I got here, after reading some relpies I will leave it as is, I see now, that it’s just Linux doing it’s job.

    In other words, use that memory , use it up, don’t need to keep reading commonly used code from disk. It does work nice as a File and Print server. I haven’t noticed any lagg, so why stress? It’ll clear out what it needs, when it needs it.

    Though getting a stick of RAM over a gig should be the next thing I do to it. It’s got a 2.06 GHZ 32bit processor, it’s lacking on RAM.

  • Pingback: Fix Clear Cache Memory Errors - Windows XP, Vista & Windows 7, 8()

  • Pingback: How to Clear Memory Cache on Linux | Unixmen | My WebSite()

  • Pingback: How To Fix Clear Cache Memory Xp Errors - Windows Vista, Windows 7 & 8()

  • Pingback: How To Fix Clear Cache Memory Linux Errors - Windows Vista, Windows 7 & 8()

  • Pingback: How To Fix Clear Cache Memory Vista Errors - Windows Vista, Windows 7 & 8()

  • Pingback: How to Clear Memory Cache on Linux | Unixmen | Peter Csontos Atrium()