Tuesday, April 30, 2024
5
rated 0 times [  5] [ 0]  / answers: 1 / hits: 853  / 3 Years ago, sat, july 10, 2021, 9:44:16

I wanted to write a script to check my RAM usage at regular intervals and then calculate its maximum.


To calculate the used Memory, I had two options, via neofetch or free -m


The output of $ free -m gives:


              total        used        free      shared  buff/cache   available
Mem: 15839 4169 7630 794 4039 10555
Swap: 2047 0 2047

So, I piped the output as $ free -m | grep Mem | awk '{print ($3/1024)}' which results in 4.07324 .


Now, if I do something similar to $neofetch output as, $ neofetch | grep -i Memory | awk '{print ($2/1024)}', I get the output as 4.84668


Thus, these 2 outputs do not match up, which one is to be considered for checking the actual RAM usage?
If it matters, I have a 4gb GTX1650 running Nvidia drivers, and consuming 53MB RAM when checked via $ nvidia-smi




One of the possible situations I can think of, is where $ neofetch gives me the Used memory and the shared memory, since the outputs of


$ free -m | grep Mem | awk '{print (($3+$5)/1024)}'


and


$ neofetch | grep -i Memory | awk '{print ($2/1024)}'


are very close to each other (4.85547 and 4.86035)


But my question remains, which memory values to trust?


More From » command-line

 Answers
1

I would recommend using free -m or cat /proc/meminfo for your source of memory usage. These numbers come directly from the kernel.


Here, the number used memory is calculated as:


# used = total - free - buffers/cache

Neofetch calculates memory usage from cat /proc/meminfo, and as you guessed, it shows
used memory including the "shared" part: (source code, line 2679):


# MemUsed = Memtotal + Shmem - MemFree - Buffers - Cached - SReclaimable

I believe the number from free is the most reliable. But since memory usage can be complex, there isn't a simple answer. You'll have to ask the developers of Neofetch why they have calculated it as they do.


[#244] Sunday, July 11, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
giccolla

Total Points: 161
Total Questions: 124
Total Answers: 117

Location: Aland Islands
Member since Wed, Nov 17, 2021
3 Years ago
giccolla questions
;