Monday, 20 March 2017

Linux system’s RAM description



What's going on?
Linux is borrowing unused memory for disk caching. This makes it looks like you are low on memory, but you are not! Everything is fine! 

Why is it doing this?
Disk caching makes the system much faster and more responsive! There are no downsides, except for confusing newbies. It does not take memory away from applications in any way, ever! 

What if I want to run more applications?
If your 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! You are not low on ram! 

Do I need more swap?
No, disk caching only borrows the ram that applications don't currently want. It will not use swap. If applications want more memory, they just take it back from the disk cache. They will not start swapping. 

How do I stop Linux from doing this?
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! Disk cache makes applications load faster and run smoother, but it NEVER EVER takes memory away from them! Therefore, there's absolutely no reason to disable it! 

Why does top and free say all my ram is used if it isn't?
This is just a differing understanding of terms. 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 still available for applications?
You might consider that memory "free" or at least "available". Linux instead counts it as both "used" and "available", but not "free":
Memory that is
You'd call it
Linux calls it
taken by applications
Used
Used
available for applications, and used for something
Free (or Available)
Used (and Available)
not used for anything
Free
Free
This "something" is 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. 

How do I see how much free ram I really have?
To see how much ram is free to use for your applications, run free -m and look at the "available" column:
$ free -m
              total        used        free      shared  buff/cache   available
Mem:           1504        1491          13           0         855      869
Swap:          2047           6        2041

If you are using an older distribution that doesn't show an "available" column, look at the "free" column in the row that says "-/+ buffers/cache":
$ free -m
             total       used       free     shared    buffers     cached
Mem:          1504       1491         13          0         91        764
-/+ buffers/cache:        635        869
Swap:         2047          6       2041

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 42%! 

When should I start to worry?
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

No comments: