0

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

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
drebrb
  • 3
  • The words "free" and "used" are ambiguous here. For free, do you mean unused memory or also memory used as cache that can be easily given to other programs? For used memory, do you mean only memory being used for processes, or do you want to include cache as well? The numbers in /proc/meminfo overlap, and the numbers given by free have been fudged so people are not confused by thinking they have no memory left when it is all in cache. – user10489 Aug 26 '21 at 04:56
  • What I mean by memory used is, memory that is no longer available for use by anything because it is currently allocated elsewhere. So I have 16gb or ram on my machine, and the idea with this script is that it displays the amount of memory currently in use on my status bar as a visual reminder and warning that I am approaching the 16gb total. When I am running multiple virtual machines and browser tabs this information is helpful. – drebrb Aug 26 '21 at 05:20
  • The second and third numbers displayed by free are probably your best numbers for that. But realize, you can't use all the free memory, as the system needs a bit to work with for I/O and random backgorund tasks. – user10489 Aug 26 '21 at 05:21
  • Check free man page for details how "used" is calculated, it also says which lines from /proc/meminfo it uses. – Vojtech Trefny Aug 26 '21 at 05:26
  • Yeah I have experienced what happens when you use to much memory, not good. That's why I am posting here so I can design something that reminds me of how much memory is being used. – drebrb Aug 26 '21 at 05:28

1 Answers1

0

Given your stated purpose:

What I mean by memory used is, memory that is no longer available for use by anything because it is currently allocated elsewhere. So I have 16gb or ram on my machine, and the idea with this script is that it displays the amount of memory currently in use on my status bar as a visual reminder and warning that I am approaching the 16gb total.

I reckon your calculation matches your intent. MemAvailable measures how much memory can be made availlable at short notice (see Meaning of "available" field in "free -m" command for details), so the difference between MemTotal and that measures how much memory is not available.

You might consider using MemAvailable directly, and tracking how close it gets to 0, instead of tracking how close your value gets to 16GiB — that way you don’t need any mental gymnastics or knowledge of the amount of memory installed.

man free explains the calculations used to determine the values displayed in detail.

Stephen Kitt
  • 434,908