26

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
.
.
.
...
4
  • That depends on how accurate a picture you're wanting. For the 1.92G/5.83G stats you quote, simply take the numbers in the first two lines and convert from kB to GB (i.e. divide by 1024*1024)... Most of the rest of the lines are simply a breakdown of what's currently in use base on how it's being used, whether it's been modified or not, and a few other characteristics...
    – twalberg
    Commented Dec 19, 2016 at 16:04
  • 1
    Hi @twalberg, thanks for your reply. So you are proposing something like [MemUsed] = [MemTotal] - [MemAvailable]?
    – tgogos
    Commented Dec 19, 2016 at 16:20
  • I'm not really proposing anything, because it's not clear what kind of "picture" you're actually looking for. That might be a reasonable estimate, but it's not going to be exact - memory accounting in Linux (or really any significant OS, for that matter) is complex and not easily reduced to an equation in two variables...
    – twalberg
    Commented Dec 19, 2016 at 16:24
  • the free command dumps the same as /proc/meminfo.
    – Douglas Su
    Commented Dec 20, 2016 at 14:30

1 Answer 1

100

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

  • Total used memory = MemTotal - MemFree
  • Non cache/buffer memory (green) = Total used memory - (Buffers + Cached memory)
  • Buffers (blue) = Buffers
  • Cached memory (yellow) = Cached + SReclaimable - Shmem
  • Swap = SwapTotal - SwapFree

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

htop screenshot

6
  • How about blue? Also, what is « Buffers »?
    – nimser
    Commented Apr 27, 2017 at 3:58
  • Sorry, buffers was mislabeled yellow, it is blue. man proc says this about Buffers: "Relatively temporary storage for raw disk blocks that shouldn't get tremendously large"
    – Hisham H M
    Commented May 2, 2017 at 19:26
  • @NicolasWormser 30% of RAM is “buffers”. What is it?
    – sourcejedi
    Commented May 7, 2019 at 20:21
  • 3
    Why do you subtract shared memory from the cached memory (Cached + SReclaimable - Shmem)? The top command doesn't seem to do this for its buff/cache calculation.
    – Matt K
    Commented Oct 11, 2019 at 18:24
  • @MattK, if I had to guess, it's probably because Shmem is included in the number for Cached, and he feels that it shouldn't count as true cached memory.
    – JacobTDC
    Commented Mar 5, 2020 at 0:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.