Clear Cache, Buffer, And Swap Space

Many Windows users will want to clear cache, buffer, and swap space. It’s a holdover from their Windows days, and it’s possible to do those things with Linux. This article will explain how to clear your RAM cache, buffer, and swap space.

One of the first questions we should be asking is, “Will this help?” The answer is, like many things, “Well, it depends…” It depends on a number of variables, like how much RAM you have, and how aggressively the kernel is paging, or what your swap preferences are. For example, quite a few people have decided to forgo swap entirely. Cleaning swap space is not going to help them out one bit.

The second question should be, “What exactly are we cleaning, anyhow?” The answer to that is a bit clearer, thankfully. The things we’ll be dealing with are:

  1. PageCache/Page Cache; The Linux kernel stores data in unused sections of memory in case it needs it again. Since kernel 2.2, Buffer Cache and Page Cache have been combined and there’s just PageCache.
  2. Dentries; They’re “the in-memory representation of a directory entry” and include things like meta data.
  3. Inodes; Meta data about all the files on your mounted file systems.

Those things build up in RAM and they can be cleaned out. The Linux kernel is really good at managing these things and unused RAM is wasted RAM (within reasonable limitations), but they can be cleaned out and this article explains how.

Clear Cache, Buffer, And Swap Space:

This article requires an open terminal. To open one, you can just use your keyboard – press CTRL + ALT + T and your default terminal should open right up!

I took the time to explain what you’ll be cleaning in the article’s opening. That’ll make this section easier!

To clear PageCache by itself:

Or you can clear Dentries and Inodes:

Or even clear PageCache, Dentries, and Inodes at once:

NOTE: These work as advertised. Before you run one of these commands, run free -m before and after running the command. By doing that, you can see what the results are and what the results will probably look like in the future.

Frankly, I’d only see this as a very valuable tool if you have little RAM, or want to free up some resources before opening something that is resource-intensive. Other than that, just let Linux do its job – managing cache, memory, and other resources.

But, if you’re a Windows user and want the comfort of some familiarity, this won’t harm anything and could provide the very slightest of benefit. You could even alias it to an easier-to-type command and run it as often as you want. It’s your computer, you do what you want with it!

Closure:

There it is, another article! We’re rapidly approaching the 6 month mark and so far things have gone well. This time, you get an article letting you know how to clear cache, buffer, and swap. Who knows what the next article will be?

Thanks for reading! If you want to help, or if the site has helped you, you can donate, register to help, write an article, or buy inexpensive hosting to start your own site. If you scroll down, you can sign up for the newsletter, vote for the article, and comment.

Subscribe to Newsletter!
Get notified when new articles are published!
We promise to never share your email!
icon

Author: KGIII

Retired mathematician, residing in the mountains of Maine. I may be old and wise, but I am not infallible. Please point out any errors. And, as always, thanks again for reading.

6 thoughts on “Clear Cache, Buffer, And Swap Space”

  1. I am an experienced Linux user but have some doubt on certain parts. Below are the questions:
    As you have already mentioned that the Linux kernel is already good at managing the free memory, then wWhat is the benefit of clearing the cache?
    After writing 1,2 or 3 to the procfs entry of /proc/sys/vm/drop_caches does the value be restored back to default?
    What is the performance impact after clearing the cache(s)? Will the kernel cache again?

    1. Thanks for commenting and asking. I’ll see what I can do to further explain.

      Yeah, there’s really not much benefit. You could clear the cache before opening a large program or you could clear the cache while you have a lot of RAM-intensive stuff open and free up a little. This would really only be of benefit when you’re using a system with little RAM. If you’ve only got 4 or even 8 GB of RAM, it might be useful to run the command should things slow down. Linux will deal with it itself, but you can tell it to do so on your terms and in your own time, rather than waiting for things to get paged in and out of RAM (or the likes).

      The kernel does a fine job at managing memory, but these features are there for a reason.

      After you’ve run any of these commands, the system will keep functioning just like it did before you ran them. It’s not a permanent setting that you’re changing, it’s just a single-instance command. Think of it like running ‘inxi’. Once you run it and get the results, inxi isn’t still running and nothing has changed on your system.

  2. I haven’t read anything here that I disagree with. As a whole the underlying OS typically is solid on memory management. My experience with memory issues in linux have related to memory leaks cause by various applications running on top of an OS. Watching a production environment its important to keep an eye on the baseline. I think the key is to understand what normal looks like as well as watch for things likely to cause issue. On may occasions I have had to recycle an application or even a system to temporarily resolve issue of over consumption of resources while working with a vendor to get a fix. With all that said I have never seen any cache / swap clearing regiment that made any sense

  3. Thank KGII for such a beautiful explanation, especially the “inix” example.

    Now, for the first context of loading a large sized app, user need not to worry about the RAM at all, right?
    The kernel itself will page out occupied RAM memory wither from the buffered zone or the in-memory apps sitting idle based on the page replacement algo.

    But after writing the above, I have found something from the below article:
    https://www.tecmint.com/clear-ram-memory-cache-buffer-and-swap-space-on-linux/

    The article explains what Page cache is.
    Basically page cache is the cached memory of the swapped out memory present in the disk.
    That means Linux even keeps cached copy of swap memory.
    Pleas correct me if I am wrong here.

    The below excerpt from the above article:
    “Linux is designed in such a way that it looks into the disk cache before looking onto the disk. If it finds the resource in the cache, then the request doesn’t reach the disk. If we clean the cache, the disk cache will be less useful as the OS will look for the resource on the disk.”

    And this makes sense to me.

    1. Yes, that is correct. If you’re going to close and open the same program more than once, then cleaning cache between closing and reopening will actually slow it down. It’d probably be a fairly trivial amount, but it would mean that you’re having to dig things out of the disk instead of pulling it off RAM.

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe To Our Newsletter
Get notified when new articles are published! It's free and I won't send you any spam.
Linux Tips
Creative Commons License
This work is licensed under a Creative Commons Attribution 4.0 International License.