In the calculation below, I only use the awk command once to read a file. I believe that this is much more efficient than using the free command if one had to compare side-by-side. Perhaps, after, I could do a test to find out?
Here is what I have so far:
Code: Select all
awk '/MemTotal/ {total=$2} /MemFree/ {free=$2} /Buffers/ {buffers=$2} $1 ~ /^Cached/ {cached=$2} END {printf "%.0f\n",(total - (free + buffers + cached))/1000}' /proc/meminfo
awk grabs MemTotal, MemFree, Buffers (has to make sure it matches the whole word or it grabs the wrong number), and Cached (again, match the whole word...). The calculation used is: MemTotal - (MemFree + Buffers + Cached) as per https://access.redhat.com/solutions/406773. The result is divided by 1000 to make human readable (should it be 1024 or another number?) Then rounded to 1s (i.e., "%.0f/n").
Code: Select all
$ cat /proc/meminfo
MemTotal: 3914524 kB
MemFree: 2573412 kB
MemAvailable: 2987780 kB
Buffers: 34068 kB
Cached: 937708 kB
SwapCached: 0 kB
Active: 524700 kB
Inactive: 760272 kB
Active(anon): 319028 kB
Inactive(anon): 175120 kB
Active(file): 205672 kB
Inactive(file): 585152 kB
Unevictable: 0 kB
Mlocked: 0 kB
SwapTotal: 4062204 kB
SwapFree: 4062204 kB
Dirty: 48 kB
Writeback: 0 kB
AnonPages: 308904 kB
Mapped: 120608 kB
Shmem: 180960 kB
Slab: 34704 kB
SReclaimable: 23920 kB
SUnreclaim: 10784 kB
KernelStack: 1472 kB
PageTables: 4196 kB
NFS_Unstable: 0 kB
Bounce: 0 kB
WritebackTmp: 0 kB
CommitLimit: 6019464 kB
Committed_AS: 711844 kB
VmallocTotal: 34359738367 kB
VmallocUsed: 0 kB
VmallocChunk: 0 kB
AnonHugePages: 83968 kB
ShmemHugePages: 0 kB
ShmemPmdMapped: 0 kB
DirectMap4k: 46580 kB
DirectMap2M: 4018176 kB
Comparing to the 'free' command:370
Code: Select all
$ free -m
total used free shared buff/cache available
Mem: 3822 337 2512 176 972 2916
Swap: 3966 0 3966
Sorry about the long post, but I wanted on the info there at once. As well, just to make this more complex than it already is, I am using Transparent Huge Pages set to [Always] if that has any effect on this? It is kernel enabled.
A reading closer to the one used with the htop command might be attained with (as per https://codedump.io/share/eSQbQNrsUB8O/ ... -like-htop):
Code: Select all
awk '/MemTotal/ {total=$2} /MemFree/ {free=$2} /Buffers/ {buffers=$2} $1 ~ /^Cache/ {cached=$2} /SReclaimable/ {reclaim=$2} /Shmem:/ {shmem=$2} END {printf "%.0f", ((total - free) - (buffers + cached + reclaim - shmem)) / 1000}' /proc/meminfo