The Buffer/Cache Phobia

The Buffer/Cache Phobia

As a Linux Support Engineer, I often get a question from People: "Why Linux is using up my Ram as Buffer/Cache?". This article is an attempt to kill this phobia related to Linux Memory Management.

The Phobia:

When I run "free" command, I see certain amount of memory consumed by Buffer/Cache. I do not like this. I want to see more memory under the column "free" or less memory under the column "used".

The Concept:

Memory is used in Computer Systems due to one single reason: Data on Memory can be accessed (Read/Write) much faster than that on Disk.

Demonstration:

For easier explanation, I am taking a Ram with 3 unit memory. My free output under no memory usage (just an hypothesis) would be:

I use four Apps in my machine: Thunderbird, Firefox, Wireshark and TigerVNC. Lets Suppose each App require one unit of Memory (This is to make the explanation simple, in real-case we will be dealing with "Threads" and "Pages")

Consider read speed from disk as X and Ram I use is 10 times faster (speed=10X).

If I open 3 Apps (Say Thunderbird, Wireshark and Firefox), the system has to read the data for the Apps from disk at a speed of X.

My free output would be:

After sometime of browsing, I close Firefox.

Case 1: In a world without "Cache".

Firefox would be discarded from the memory and the free output would look like:

Case 2: In real world (one with caching), Firefox will stay in Memory as cache and the free output would look like:

See the "used" column still shows 3. People get panic due to this.

Now suppose I need to open Firefox again.

In case 1: My system has to read the data again from the disk with a speed X.

In Case 2: Firefox is still in memory and can be accessed with much faster speed 10x. Yes, You panicked in the second case for this Paradox!!!

Suppose, I am opening another App (Say TigerVNC) instead of Firefox. In case 1, we have 1 unit memory free. This can be readily used by the App.

In case 2, there is no free memory unit for TigerVNC. Was the phobia reasonable after-all? The answer is NO. Linux is smart enough to know that there is no memory free for new App and therefore it discards Firefox from cache and make space for TigerVNC.

When do I need to start Worrying:

You should worry when both "free" and "cache/buffer" is zero. This will happen if 3 Apps (Thunderbird, Wireshark and TigerVNC) are running in the example mentioned on this article. If you need to open Firefox now, there is no memory unit available for the App. Linux will call its hit-man friend OOM killer now to take a decision on this.It may kill one app and make space.

How OOM killer choose his victim is out of scope of this article.

Bottom-line:

You do not need to panic if you see "used" section in "free" command output show high value due to "Cache/Buffer". Stay calm and thank Linux for making your App readily available for faster access (10X in our example).

Wise Man once said: Empty RAM is “wasted RAM”, let it cache!




To view or add a comment, sign in

More articles by Askar Ismail

  • Evolution Of Containers

    I have been planning to write this down, since I first met docker back in 2015. One of my friends introduced me to it…

    3 Comments
  • Threats of the Virtual World

    The world has now evolved into a virtual one. Internet has brought drastic changes in our life over the past few…

Others also viewed

Explore content categories