0

I'm trying to calculate the real memory usage for my MOTD and also front OLED display in bash.

Emberassingly I didn't convert the numbers from kB properly to MB. But know that the formula from /proc/meminfo is:

MemTotal - MemFree - Cached - Buffers

I was able to extract them using grep -e. But I'm stuck with the following:

  • How can I grep only the values from /proc/meminfo?
  • How can these numbers be converted from kB to MB (/1024)?

So my task is to subtract the values as mentioned above and have this value be printed as a single value (appending MB is a bonus).

1
  • 2
    Please edit your question to include a Minimal, Reproducible Example with concise, testable sample input, expected output and your attempt to solve the problem yourself so we can help you further. See How to Ask and look at existing questions that have been upvoted and answered for examples.
    – Ed Morton
    May 9 at 22:41

1 Answer 1

0

With GNU sed you can do domething like

sed -zE 's_.*MemTotal: *([0-9]*).*\nMemFree: *([0-9]*).*\nBuffers: *([0-9]*).*\nCached: *([0-9]*).*_echo $(((\1-\2-\3-\4)/1024)) MB_e' /proc/meminfo

The -z option reads the whole file proc/meminfo in a single buffer to extract all four values in a single regular expression and form the calculation out of it to use the dangerous e flag of the s command to execute the command.

For POSIX sed versions without -z and without e flag, you can fill the sed extraction in your calculation by command substitution (equally dangerous):

echo $(( ($(sed -E '/^(MemTotal|MemFree|Cached|Buffers): *([0-9]*).*/{s//\2/;H;};$!d;x;s/[[:cntrl:]]//;s//-/g' /proc/meminfo)) / 1024)) MB

I know the lines are long, but both are one-liners somehow.

Update: Different rounding

In the comment there was a request to first convert each value to MB prior to substraction. Here is the modified script:

echo $(( $(sed -E '/^(MemTotal|MemFree|Cached|Buffers): *([0-9]*).*/{s//\2/;H;};$!d;x;s/[[:cntrl:]]//;s__/1024-_g;s_$_/1024_' /proc/meminfo))) MB

Please note that this results in additional rounding errors. For example

MemTotal:       16359572 kB
MemFree:         6614000 kB
Buffers:          602108 kB
Cached:          3685372 kB

will output 5333 MB instead of 5330 MB (true value: 5330.16 MB).

6
  • Dear Philippos. Thank you so much so far with these answers! While i don't mind the dangerous aspect. I do have to ask one small change if this is possible. I know that technically saying: x/1024-y/1024 etc can be denoted as (x-y)/1024. However, testing this in excel and batch i noticed i was getting very close to the reported number, but not quite. This has to do with the rounding off that happens at each stage (making all kB's mB's makes the end reported memory much smaller then the current solution. Would it be possible to /1024 each value first, and then substract? Thank you so much!
    – Macley
    May 10 at 18:45
  • You can modify the script that way, of course, but the rounding error will be much worse if you switch to MB first.
    – Philippos
    May 11 at 7:17
  • Dear Philippos, true, but this will result in the displayed value mobaxterm is giving me (and that i'm getting from grafana aswell).
    – Macley
    May 11 at 19:50
  • Pardon for sending two replies, i hoped enter would've made a new line. I tried to modify you script by: sed -zE 's_.*MemTotal: ([0-9]).*\nMemFree: ([0-9]).*\nBuffers: ([0-9]).*\nCached: ([0-9]).*_echo $(((\1/1024-\2/1024-\3/1024-\4/1024))) MB_e' /proc/meminfo or echo $(( ($(sed -E '/^(MemTotal|MemFree|Cached|Buffers): *([0-9]*).*/{s//\2/;H;};$!d;x;s/[[:cntrl:]]//;s//-/1024/g' /proc/meminfo)) )) MB but i seem to get an extremely high number or a syntax error. Would it be possible to modify the given solutions with the /1024 on each fetched value? I'm really sorry for not understanding.
    – Macley
    May 11 at 19:52
  • If you use / as delimiter for the s command, you need to escape any literal / with a backslash. Or you use a different delimiter like I did in my updated answer.
    – Philippos
    May 12 at 6:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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