Jump to content

RAM usage report in the welcome script different than free -m


sibianul

Recommended Posts

Hello guys,

 

I'm just starting with bash scripts and the bananaPi, and I'm curious how the developers came up with the value of RAM usage,  and why in the welcome script that loads when I login to SSH, the RAM usage is different from the command free -m ?

 

I don;t really understand the line about memory_usage :) I just noticed the result is different from the free -m

 

Thanks

# memory and swap
mem_info=$(LANG=en_US.UTF-8 free -w 2>/dev/null | grep "^Mem" || LANG=en_US.UTF-8 free | grep "^Mem")
memory_usage=$(awk '{printf("%.0f",(($2-($4+$6+$7))/$2) * 100)}' <<<${mem_info})
memory_total=$(awk '{printf("%d",$2/1024)}' <<<${mem_info})
swap_info=$(LANG=en_US.UTF-8 free -m | grep "^Swap")
swap_usage=$( (awk '/Swap/ { printf("%3.0f", $3/$2*100) }' <<<${swap_info} 2>/dev/null || echo 0) | tr -c -d '[:digit:]')
swap_total=$(awk '{print $(2)}' <<<${swap_info})

 

ram_usage with free -m.jpg

ram welcome message.jpg

Link to comment
Share on other sites

That's right... it looks like it's taking into account the buffers, shared and cached memory.

 

Before I can break it down, can you give the output of free -w, as it displays it slightly differently. if you can get the motd in shot that would be great also, as it would let me confirm the maths ;) I'm off to bed now, so it will be a while before I reply ;)

 

To give you another example for comparison:

 

 

 

armbian-memory-usage.png

Link to comment
Share on other sites

It seems I don't have an -w parameter , I get an error, again here is the free -m

 

In the mean time I have stopped both services that clamav had and the used memory dropped alot, as you can see bellow


 

/etc/init.d/clamav-daemon stop
/etc/init.d/clamav-freshclam stop


root@banana:~# free -w
free: invalid option -- 'w'

root@banana:~# free -m
             total       used       free     shared    buffers     cached
Mem:           999        534        464         39         18        301
-/+ buffers/cache:        214        784
Swap:          127          0        127
root@banana:~# free -V
free from procps-ng 3.3.9

 

Link to comment
Share on other sites

3 hours ago, sibianul said:

($2-($4+$6+$7))/$2)

$2 --> total

$4 --> free

$6 --> buffers

$7 --> cache

 

Basically it's substracting free+buffers+caches from total since used and (only) free are stupid metrics on a Unix like system like Linux. Having any 'free' memory is either a sign you just booted the box or that something is seriously wrong. The kernel uses every available RAM for reasonable stuff and that's buffers and caches. Those get flushed immediately when needed so that's the only reasonable variant to show 'memory available to tasks that need it' (remember: buffers and caches get flushed on demand as soon as other tasks want to allocate more memory).

 

The value Armbian shows here is pretty close to what you get when emptying caches as root and then check 'free' and 'used' memory:

echo 3 > /proc/sys/vm/drop_caches
Link to comment
Share on other sites

Just looked into my monitoring scripts (snmpd on the local machines feeding OpenNMS). On Linux we use

vmstat -s | awk -F" " '/used memory$/ {printf("%.0f",$1/1024)}'

This is not a percentage value but the above calculation already done by good old friend vmstat. The syntax above in post #1 is from me but I just changed Igor's old motd scripts to tune them for faster execution a while ago. In such situations I never adjust the script's logic since this would be worst case: a faster script producing wrong numbers.

Link to comment
Share on other sites

Hy again,

 

now some numbers in atop are red, after a few days of the BM1 running, when I login to SSH it still says that the Memory usage is 26 % of 999MB  .. but when running atop, check the screenshot.

 

Is everything ok ? All the red numbers are related to SWAP ?

 

I can see mysql on top of the list VSIZE  558Mb, anyone has any suggestions if I can do anything to lower the memory ussage, or I should not worry about it, will the BM1 run smoothly for an year ? I want to have it store the values of all kind of sensors in my house, every 1 minute or 5 minutes for other devices, I also want to install an SATA drive, just need to solder some wires for the 12v and 5v power to the HDD (and also power the BM1 from the same 5v source) 

 

Thanks. Any advices are welcome.

atop.jpg

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
×
×
  • Create New...

Important Information

Terms of Use - Privacy Policy - Guidelines