I've read some articles about Linux memory management and understand (perhaps not proficient) the concept of free memory, available memory, swap, etc. But when I check with the following command:
cat /proc/meminfo; free -w; vmstat -s
I just can't calculate the numbers precisely to prove my understanding.
For example, "cat /proc/meminfo" shows:
MemTotal: 8169968 kB
MemFree: 984124 kB
MemAvailable: 4231224 kB
Buffers: 171420 kB
Cached: 3272688 kB
SwapCached: 65604 kB
Active: 3415076 kB
Inactive: 3388312 kB
Active(anon): 2636088 kB
Inactive(anon): 789932 kB
Active(file): 778988 kB
Inactive(file): 2598380 kB
Unevictable: 64 kB
...
"free -w" shows:
total used free shared buffers cache available
Mem: 8169968 3566616 983864 66952 171420 3448068 4230964
Swap: 3903484 500620 3402864
"vmstat -s" shows
8169968 K total memory
3566616 K used memory
3415476 K active memory
3388312 K inactive memory
983864 K free memory
171420 K buffer memory
3448068 K swap cache
3903484 K total swap
500620 K used swap
3402864 K free swap
9335996 non-nice user cpu ticks
...
And here are my questions:
1. Why doesn't "used + available = total"?
Some articles say that "free" memory is not used by anything, while "available" memory might be used by, for example some buffer, but can be allocated to programes at any time. As per my understanding, it means:
total = available + used
But when I calculate:
4231224 ("MemAvailable" in /proc/meminfo) + 3566616 ("used memory" in vmstat) = 7797840
Gap to "total" (8169968 K): 372128 K (or 363 M) less
4230964 ("available" in free) + 3566616 ("used" in free) = 7797580
Gap to "total" (8169968 K): 372388 K (or 363 M) less
So why is there a large gap?
2. Why doesn't "free + buffer + cache = available"?
According to some articles, Linux may use some memory for buffer and cache, so this part of memory are not "free" (not used by anything, so it can be allocated to programes immediately) but still "available" (despite being used for buffer or cache, but can be freed at any time and allocated to programes). But when I calculate:
/proc/meminfo:
984124 (MemFree) + 171420 (Buffers) + 3272688 (Cached) = 4428232
Gap to "MemAvailable" (4231224): 197008 K (or 192 M) more
free:
983864 (free) + 171420 (buffers) + 3448068 (cache) = 4603352
Gap to "available" (4230964): 372388 K (or 363 M) more
Why is there a large gap?
Furthermore, number "372388 K" occured in both this and the last question. That is, in "free" command:
total = available + used + 372388
total = free + buffers + cache - 372388
So what is it (the "372388 K" memory)?
3. Why doesn't "active + inactive + free = total"?
Some articles say that "active" is memory accessed recently, while "inactive" is that not. As per my understanding:
active (application) + inactive (application) = memory used by applications active (buffer) + inactive (buffer) = memory used by buffer ...
So "active + inactive = total used memory" and "total used memory + free = total memory". But when I calculate:
/proc/meminfo:
3415076 (Active) + 3388312 (Inactive) + 984124 (MemFree) = 7787512
Gap to total (8169968 K): 382456 K (or 373 M) less
vmstat:
3415476 (active) + 3388312 (inactive) + 983864 (free) = 7787652
Gap to total (8169968 K): 382316 K (or 373 M) less
Is my understanding correct? Are there any other similar equations?
The reason I'm asking this question is not for writing an application to calculate memory, but rather for validating my understanding and finding out what I missed.
For example if I was told that my salary is $1000 per month but got just $800 on my bank accout. Some articles say that the reason is tax. I calculated 12% tax but there is still $80 gap, so I asked again and then got to know the concept of social insurance.
Output of those Linux memory statistics is such a case: except that "free swap + used swap = total swap" can be precisely calculated, I can't get any equation from the output. This makes me unconfident with my understanding of Linux memory management. I think I've missed something, especially when the gap is up to 373 MB.