linuxmemoryhtop

How to calculate system memory usage from /proc/meminfo (like htop)


Running the htop command gives you a picture of the memory usage in a format like this:

1.92G/5.83G

Question: how should I interpret the values taken from /proc/meminfo in order to calculate programmatically the memory used?

I am looking for something similar to this: Accurate calculation of CPU usage given in percentage in Linux? meaning that pseudocode is ok, I do not need something that can be compiled, just the logic. The source code of htop is a place to look for but I had no luck spotting the lines of code written for this...

$ cat /proc/meminfo 
MemTotal:        6110716 kB
MemFree:         2076448 kB
MemAvailable:    3800944 kB
Buffers:          382240 kB
Cached:          1496216 kB
SwapCached:            0 kB
Active:          2830192 kB
Inactive:         796648 kB
Active(anon):    1749940 kB
Inactive(anon):   109808 kB
Active(file):    1080252 kB
Inactive(file):   686840 kB
Unevictable:          48 kB
.
.
.
...

Solution

  • htop author here. These are the calculations I make to get the numbers for the green, blue and yellow bars in the memory meter:

    In the htop source code: linux/LinuxProcessList.c and linux/Platform.c.

    htop screenshot