I am trying to as accurately as possible calculate the amount of ram being used by the system at any given time. I figured that if I subtracted MemAvailable from MemTotal and divided the difference by 1024 to convert from kB to MiB that would give me the total amount of ram currently being used. Based on the output below from /proc/meminfo when I run my script the output is 1905.27 MiB
. However, both free -m and top report memory used as 1154 MiB
. Am I missing something? Why does my output differ? Is my approach not the correct way to calculate total memory in use?
Output from /proc/meminfo:
MemTotal: 16093472 kB
MemFree: 11284772 kB
MemAvailable: 14142472 kB
Buffers: 256672 kB
Cached: 3217852 kB
SwapCached: 0 kB
Active: 1721676 kB
Inactive: 2375840 kB
Active(anon): 3928 kB
Inactive(anon): 1057160 kB
Output from free -m
:
total used free shared buff/cache available
Mem: 15716 1154 11014 428 3547 13803
Swap: 2047 0 2047
Snippet from the script:
mem_avail() {
awk 'FNR == 3 {printf "%lu", $2}' "/proc/meminfo"
}
mem_total() {
awk 'FNR == 1 {printf "%lu", $2}' "/proc/meminfo"
}
mem_used() {
echo "| $((($(mem_total)-$(mem_avail))/1024)) MiB"
}
My output based on the values from /proc/meminfo: 1905.27 MiB
/proc/meminfo
it uses. – Vojtech Trefny Aug 26 '21 at 05:26