Thursday, December 26, 2024
HomeLinuxLinux Memory and Disk Caching Explained

Linux Memory and Disk Caching Explained

By Default, Linux is borrowing unused memory for disk caching. Sometimes it makes it looks like your memory is low, practically, it’s not.  Disk caching makes the system much faster and more responsive. It makes sure there are no downsides. It does not take memory away from applications in any way, you think.

 Not always we run few applications, but sometime we have run more applications or some of our applications want more memory, they just take back a chunk that the disk cache borrowed. Disk cache can always be given back to applications immediately and this make sure it doesn’t feel like its running out of memory.

And disk caching only borrows the ram that applications doesn’t need for time being. It never uses swap memory. If applications want more memory, they just take it back from the disk cache. They will not start swapping.

Can we disable Disk Caching?

No, you can’t disable disk caching. The only reason anyone ever wants to disable disk caching is because they think it takes memory away from their applications, which it doesn’t do that. Disk cache makes only applications load faster and run smoother, it never takes memory away from it, so there is no point to disable it.

Output Comparison of TOP and FREE

This is just a difference in terminology. Both you and Linux agree that memory taken by applications is “used”, while memory that isn’t used for anything is “free”.

But how do you count memory that is currently used for something, but can still be made available to applications?

You might count that memory as “free” and/or “available”. Linux instead counts it as “used”, but also “available”:

Memory that is You’d call it Linux calls it
used by applications Used Used
used, but can be made available Free (or Available) Used (and Available)
not used for anything Free Free

This “something” is (roughly) what top and free calls “buffers” and “cached”. Since your and Linux’s terminology differs, you might think you are low on ram when you’re not.

Free Command Explained.!!

To see how much ram your applications could use without swapping, run free -m and look at the “available” column:

$ free -m
              total        used        free      shared  buff/cache   available
Mem:           4951         392        2392           2        2165        4193
Swap:          2043         184        1859

This is your answer in megabytes. If you just naively look at “used” and “free”, you’ll think your ram is 99% full when it’s really just 47%!

Read More: /proc/meminfo: provide estimated available memory

Memory Thresholds

A healthy Linux system with more than enough memory will, after running for a while, show the following expected and harmless behavior:

  • free memory is close to 0
  • used memory is close to total
  • available memory (or “free + buffers/cache”) has enough room (let’s say, 20%+ of total)
  • swap used does not change

Warning signs of a genuine low memory situation that you may want to look into:

  • available memory (or “free + buffers/cache”) is close to zero
  • swap used increases or fluctuates
  • dmesg | grep oom-killer shows the OutOfMemory-killer at work

Read More: How to increase SWAP partition on Linux

RELATED ARTICLES
- Advertisment -

Most Popular

Recent Comments