2

I need to display in GUI for operator used memory vs available on Linux Server

So what would be logically correct value to display as usage?

[root@host ~]# free
              total        used        free      shared  buff/cache   available
Mem:      131753676   110324960     1433296     4182648    19995420    16240640
Swap:       2097148      652076     1445072

used or (total-available)?

Difference is: 110324960 vs 115513036 or 5188076 kb ~= ~5 GB So what are with these 5 GB are they effectively used or available or unavailable and nor used? What is more correct to display for used in memory usage %?

This is for CentOS 7.3 PC, running 2 java services

But there is totally different picture on PostgreSQL Server

[root@postgres_server1 ~]# free
              total        used        free      shared  buff/cache   available
Mem:      131753684     7364056    77736740    15598120    46652888   107942020
Swap:       2097148           0     2097148

where difference between used and (total-available) is much larger: 16447608 kb ~=15.7 GB

ALZ
  • 931

2 Answers2

3

It depends what your usage is supposed to reflect. In free’s output:

  • “used” is calculated as “total – free – buffers – cache”, so it reflects the amount of memory currently storing useful data, excluding cache;
  • “available” is supposed to be the amount of physical memory which can be immediately made available for other uses.

“Total – available” would mean something along the lines of “physical memory currently in use, and non-replaceable”. The difference between that and “used”, the 5GB and 15GB you mention, is the amount of physical memory currently storing data which is not yet available elsewhere, i.e. dirty buffers (so your PostgreSQL has more data waiting to hit the disk). “Available” reflects the maximum amount of physical memory a program can request without being forced to swap (although nothing guarantees that, if a program were to use that much memory, it wouldn’t swap anyway, given the rest of the system’s behaviour at the time).

So your two values both reflect used memory, with slightly different definitions. Which one you use is up to you (or your requirements). “Available” is more accurate than “used”, so it’s probably a more useful value if you only want to keep one.

Another way to think about it is to consider what questions the values answer:

  • “used” tells you whether the 128GiB of RAM are useful for your current workload;
  • “available” tells you how much capacity you still have left;
  • “total – available” tells you how much physical RAM you really need (i.e. how much smaller your next server or VM could be, if you’re willing to lose the performance gains from cache).

There’s no perfect compromise, which is why free shows both values.

Memory immediately available in linux memory management system and How can I get the amount of available memory portably across distributions? provide more detail on the exact meaning of “available”. It isn’t equivalent to “buff/cache” because the latter includes memory which isn’t reclaimable (because it hasn’t been written to disk yet), and there are other memory pools which are reclaimable but aren’t counted in “buff/cache”.

Stephen Kitt
  • 434,908
  • what do you mean in The difference between that and “used” is the amount of physical memory currently storing data which is also available elsewhere, typically on disk. Is it currently my used SWAP or is it RESdient in RAM data and it is non-reaplaceable? – ALZ Nov 07 '19 at 09:19
  • and since it is not available for other processes could I count it as used? I need to display memory usage in percentage of total available RAM in Machine – ALZ Nov 07 '19 at 09:29
  • I've added some more details in question and data from other server – ALZ Nov 07 '19 at 09:36
  • The difference with your PostgreSQL server can be explained by the large amount of cache used on the latter. – Stephen Kitt Nov 07 '19 at 09:42
  • Also free's "used" is (currently) bogus because it doesn't include "shared". You can fill up tmpfs/ramfs and hog a whole lot of memory, and yet it won't show up in "used". – sourcejedi Nov 07 '19 at 10:22
  • another question - so why not all from buff/cache are available? – ALZ Nov 07 '19 at 11:51
  • Buff/cache includes memory which isn’t reclaimable; see the paragraph I added to the end of my answer. – Stephen Kitt Nov 07 '19 at 15:39
  • @StephenKitt, thanks a lot fro added details. Now I've really understood it! – ALZ Nov 09 '19 at 14:52
1

In addition to the existing answer:

This "simple" question touches a very important topic: memory management. This is even the main "subsystem" of the kernel. "Buy more RAM" is still universally true. But no matter how much you have: every OS has to handle the case where RAM sharing turns into congestion, into swapping, into thrashing.

There is a fine line between efficient use and running out of resources. I went through your numbers and have this interpretation:

java:

"used" shows 83% of "total". That is high. And swap is active; very small, and still with free space.

"free" shows the problem. Less than 1%. Where are the 16%? It's the so called "available", 16 GB here, which is about 16%.

"shared" (4G) plus "buff/cache" (19G) are a bit more than than these "available" 16G -- reflecting the kernel could "sacrifice" two thirds of it. But the way java is running on the system now, this is how it turns out.

So this missing sixth, the x = total - (used+free) (132G - (110+1)), is here 21G, which is between "avail" (16G) and "sh" + "b/c" (23G together).

You have to read free from left to right, first check "swap", and then ask HOW is it used: as "shared" and/or "buff/cache", and how much of that is "available".

Activation of swap and "free" under 1% show red light, but 16% available and swap details show it is "ok".

(I think the swap is too small for 132G; I hope that java service is also doing something)


SQL example:

This show how it "should" be: enough RAM and a application and OS working together:

Only 5% are used. But 50% are not free. They are used as "shared" and reserved as "buff/cache". Swap is not active. Again, you can't get all of the 61G, but a big part of it.


No use defining new words - it is a matter of interpretation. In the end it is the kernel who knows, and free is perfect for the top level overview. You have to ask "operator" what he wants, now with this information, or maybe now you even know without asking.