bash script memory size in gb

About writing shell scripts and making the most of your shell
Forum rules
Before you post please read how to get help
Zgrywus
Level 1
Level 1
Posts: 10
Joined: Mon Jul 19, 2021 3:15 pm

bash script memory size in gb

Post by Zgrywus »

Hello,
I need some help.

Want to display ram memory size, but only in GBs. Command free -m shows 3825 but command free -g shows only 3.
How to display real memory size?
User avatar
AndyMH
Level 17
Level 17
Posts: 7364
Joined: Fri Mar 04, 2016 5:23 pm
Location: Wiltshire

Re: bash script memory size in gb

Post by AndyMH »

Code: Select all

andy@T432 ~ $ free -h
              total        used        free      shared  buff/cache   available
Mem:           15Gi       1.9Gi       6.2Gi       268Mi       7.3Gi        12Gi
Swap:         7.5Gi          0B       7.5Gi
Homebrew i5-8400+GTX1080 Cinnamon 19.0, 4 x Thinkpad T430 Cinnamon 20.1, 2 x i7-3632 , i5-3320, i5-3210, Thinkpad T60 19.0 Mate
Zgrywus
Level 1
Level 1
Posts: 10
Joined: Mon Jul 19, 2021 3:15 pm

Re: bash script memory size in gb

Post by Zgrywus »

ye, it shows 15gb when you have 16gb :(
User avatar
AndyMH
Level 17
Level 17
Posts: 7364
Joined: Fri Mar 04, 2016 5:23 pm
Location: Wiltshire

Re: bash script memory size in gb

Post by AndyMH »

No it shows 15GiB when I have 16GB, different units.
Homebrew i5-8400+GTX1080 Cinnamon 19.0, 4 x Thinkpad T430 Cinnamon 20.1, 2 x i7-3632 , i5-3320, i5-3210, Thinkpad T60 19.0 Mate
mmphosis
Level 1
Level 1
Posts: 14
Joined: Sat Apr 11, 2020 11:22 pm

Re: bash script memory size in gb

Post by mmphosis »

Code: Select all

free --giga
rene
Level 17
Level 17
Posts: 7762
Joined: Sun Mar 27, 2016 6:58 pm

Re: bash script memory size in gb

Post by rene »

As to the two replies directly above; note that, no, this is not a unit issue and Andy certainly has 16GiB physical. The fact that he shows 15GiB in free -h is undoubtedly to say that that's from a system with shared-memory graphics as "losing" an entire GiB would otherwise be a lot, but not any system will display the full physical memory size there: free displays to programs available memory and will as such and for one never display kernel-reserved memory, as well as, then, things like a shared-memory framebuffer.

Best you can do as to physical memory is scrounge around dmidecode --type memory; there's too many variables as to e.g. chipset remapping to have OS-reported memory be "full-detail useable". Of course, if you settle for rounding to "nearest conceivable border" or some such you'd be fine in 99% of cases anyway but generally physical RAM-size is not trivial to get.

[EDIT] The below adds RAM regions the BIOS reports as not "reserved" during booting but added only for geek value; see above...

Code: Select all

dmesg -t | awk 'BEGIN { total = 0 } /^BIOS-e820/ { if ($0 !~ /.*reserved$/) { total += strtonum(gensub(".*-(.*)]","\\1","g",$3))-strtonum(gensub("(.*)-.*","\\1","g",$3))+1 } } END { print total }'
User avatar
Kadaitcha Man
Level 15
Level 15
Posts: 5679
Joined: Mon Aug 27, 2012 10:17 pm

Re: bash script memory size in gb

Post by Kadaitcha Man »

Zgrywus wrote:
Wed Jul 21, 2021 3:08 pm
ye, it shows 15gb when you have 16gb :(
Above, @rene posted this:
rene wrote:
Wed Jul 21, 2021 9:05 pm
there's too many variables as to e.g. chipset remapping to have OS-reported memory be "full-detail useable". Of course, if you settle for rounding to "nearest conceivable border" or some such you'd be fine in 99% of cases anyway but generally physical RAM-size is not trivial to get.
I agree with that 100%, and will reinforce what he wrote:

Any value relating to available or consumed memory is essentially meaningless in the absence of the memory being classified into:
  • Application memory
  • Buffer memory
  • Cache memory
  • Allocated memory
  • Hardware reserved memory
  • User space memory
  • System memory
  • COW memory (copy on write)
  • Kernel reserved memory (kernel, ACPI tables, etc)
  • and much more, including knowing how the kernel handles memory for every single one of those different purposes.
And, as rene succinctly put it, "physical RAM-size is not trivial to get".

Why is it so? To use just a very simple example:

The Linux kernel uses copy-on-write (COW) memory allocation, which means that two separate processes can be using the very same memory. If one of the processes attempts to modify that memory then the memory is first copied then duplicated by the kernel. This simple example directly affects the values given by different programs, and results in values in one column of output not matching those in another. To make matters even more complex, applications and the DE also use COW, so you would have to know every single memory state of every single process in order to even start to get near an accurate value.

Finally, shared memory is often excluded because it is owned by the machine, not the kernel.

The list of issues with obtaining accurate memory indicators only gets worse and far more complex from there, so to reiterate and summarise, pick a number, any number, and live with it.
[*] All kadaitcha men are liars
Zgrywus
Level 1
Level 1
Posts: 10
Joined: Mon Jul 19, 2021 3:15 pm

Re: bash script memory size in gb

Post by Zgrywus »

Is there a way to do script which shows (free -g + 1)? :P
User avatar
Kadaitcha Man
Level 15
Level 15
Posts: 5679
Joined: Mon Aug 27, 2012 10:17 pm

Re: bash script memory size in gb

Post by Kadaitcha Man »

Zgrywus wrote:
Thu Jul 22, 2021 3:05 am
Is there a way to do script which shows (free -g + 1)? :P
Yes.

Code: Select all

echo 4
[*] All kadaitcha men are liars
rene
Level 17
Level 17
Posts: 7762
Joined: Sun Mar 27, 2016 6:58 pm

Re: bash script memory size in gb

Post by rene »

Zgrywus wrote:
Thu Jul 22, 2021 3:05 am
Is there a way to do script which shows (free -g + 1)? :P
Sure, but it's then better to go through /proc/meminfo; below rounds up the value of MemTotal to the next 1G border:

Code: Select all

awk '/^MemTotal/ { print int(($2 + 1024^2 - 1)/(1024^2)) }' /proc/meminfo
Zgrywus
Level 1
Level 1
Posts: 10
Joined: Mon Jul 19, 2021 3:15 pm

Re: bash script memory size in gb

Post by Zgrywus »

rene wrote:
Thu Jul 22, 2021 3:16 am
Zgrywus wrote:
Thu Jul 22, 2021 3:05 am
Is there a way to do script which shows (free -g + 1)? :P
Sure, but it's then better to go through /proc/meminfo; below rounds up the value of MemTotal to the next 1G border:

Code: Select all

awk '/^MemTotal/ { print int(($2 + 1024^2 - 1)/(1024^2)) }' /proc/meminfo
This is what I needed! Thank you very much
1000
Level 4
Level 4
Posts: 425
Joined: Wed Jul 29, 2020 2:14 am

Re: bash script memory size in gb

Post by 1000 »

Riddle / puzzle
From man free
-m, --mebi
Display the amount of memory in mebibytes.
My value given by dmidecode and in the store is closer Mega

Code: Select all

$ sudo dmidecode --type memory | grep Size
	Size: 4096 MB
	Size: 4096 MB
	Size: 8192 MB
	Size: No Module Installed
= 16384 MB ( 16384÷1024 = 16 GB )

--mebi

Code: Select all

$ free -m
              total        used        free      shared  buff/cache   available
Mem:          15765        2116       12077         183        1570       13181
Swap:          4720           0        4720
--mega

Code: Select all

$ free --mega
              total        used        free      shared  buff/cache   available
Mem:          16530        2245       12632         198        1652       13789
Swap:          4950           0        4950
My system monitor shows only 15,4 but in GiB , not in GB
https://simple.wikipedia.org/wiki/Gibibyte
https://en.wikipedia.org/wiki/Gigabyte

15765÷1024 MiB = 15,395507813 GiB


Your 3825 MiB ÷ 1024 = 3,735351562 GiB
Command free -m shows 3825 but command free -g shows only 3.
Make sure you are looking or counting well.

I have 15765 in MiB
and I have 15 in GiB

From man free
total Total installed memory (MemTotal and SwapTotal in /proc/meminfo)
I have

Code: Select all

$ cat /proc/meminfo
MemTotal:       16143456 kB
= 15,395599365

From https://access.redhat.com/documentation ... oc-meminfo
While the file shows kilobytes (kB; 1 kB equals 1000 B), it is actually kibibytes (KiB; 1 KiB equals 1024 B).
Really error ? :shock:

1 MiB = 1.048576 MB
1 KiB = 1.024 KB
1 kibibyte is 1024 bytes
1 kilobyte originally meant 1000 bytes

From https://www.thegeekdiary.com/understand ... -in-linux/
MemTotal: Total usable ram (i.e. physical ram minus a few reserved bits and the kernel binary code)
I'm not sure. But I may be wrong.
From /proc/meminfo total 16143456 ( let's assume KiB ) × 1024 = 16530898944 bytes

Edited
https://convertlive.com/u/convert/bytes ... 6530898944
15.3956 GB
rene
Level 17
Level 17
Posts: 7762
Joined: Sun Mar 27, 2016 6:58 pm

Re: bash script memory size in gb

Post by rene »

1000 wrote:
Thu Jul 22, 2021 7:00 am
Really error ? :shock:
Not so much no. The binary prefixes are relatively new and the /proc/meminfo file relatively old; the original choice of kB has become part of the kernel ABI, i.e., you don't want to break userspace programs parsing it but yes, it's the 1024-byte "kB", i.e, KiB.

Otherwise see above; dmidecode is indeed simply by virtue of listing DIMM sizes more accurate --- unless it's not which is in the case of dmidecode often enough that you can basically never rely on it: dmidecode just spits out BIOS-filled tables and given the quality of Chinese OEM BIOSen... If you just round up MemTotal to the next higher GiB as per above you have at least 99% of cases covered.

Well, for now: I recently noticed a Zen architecture AMD APU with a by default more than 1GiB shared-memory framebuffer but, oh well.
1000
Level 4
Level 4
Posts: 425
Joined: Wed Jul 29, 2020 2:14 am

Re: bash script memory size in gb

Post by 1000 »

I don't understand something.

1 KiB = 1024 B
1 KB = 1000 B


I convert 16 GB to GiB
16 (GB) × 1000 (=MB) × 1000 (=KB) × 1000 = 16 000 000 000 (B)
16 000 000 000 (B) ÷ 1024 (=KiB) ÷ 1024 (=MiB) ÷ 1024 = 14,901161194 (GiB)


15765 (MiB total from free) ÷ 1024 = 15,3955 (GiB)

Why does the system show more than dmidecode and the manufacturer says?
15 GiB looks better?

Edit
Maybe because it's divisible by 1024 ?

15 (GiB) ×1024×1024×1024 = 16106127360 (B)
16106127360÷1000÷1000÷1000 = 16,1061 (GB)

14 (GiB) ×1024×1024×1024 = 15032385536 (B)
15032385536 ÷1000÷1000÷1000 = 15,032385536 (GB)
rene
Level 17
Level 17
Posts: 7762
Joined: Sun Mar 27, 2016 6:58 pm

Re: bash script memory size in gb

Post by rene »

Memory technology is "always" measured in the binary units. I.e., where dmidecode says "4096 MB" for a DIMM it should in modern terms say "4096 MiB", and specifically, you have 16 GiB of memory and not 16 GB.

Please note that it's not long ago that the binary units did not even exist as such and that e.g. "K" or "G" simply meant different things in different contexts; their introduction is slowly saving us from that mess but it's going to take another generation or so for the world to have caught up. I.e., the "kB" in /proc/meminfo that we saw, the "MB" in dmidecode here, ...

Being someone who's always championed using the binary prefixes I'm in fact titillated to now see already two people in this thread even thinking that a 4GB DIMM has 4000 MBs; for my own tech-generation it's deeply obvious that it has 4096 of the damn things. As obvious as how a 1Gb NIC has ability to transfer up to 10^9 bits per second, say (as in not obvious at all to anyone but my own tech-generation...).

Note; there's quotes around "always" especially due to SSDs. Technically they're memory technology, but functionally storage technology, and given that latter has for some time now used the decimal prefixes so do SSDs; the difference between the two is in fact what often accounts for a large part of an SSDs internal over-provisioning.
1000
Level 4
Level 4
Posts: 425
Joined: Wed Jul 29, 2020 2:14 am

Re: bash script memory size in gb

Post by 1000 »

Thanks.
I guess they'll have also create a name for the numbers in KiB, GiB , ...
I associate the word binary more with bag of numbers constructed with 0 and 1.

https://en.wikipedia.org/wiki/ASCII
For example, lowercase " i " would be represented in the ASCII encoding by binary 1101001
rene
Level 17
Level 17
Posts: 7762
Joined: Sun Mar 27, 2016 6:58 pm

Re: bash script memory size in gb

Post by rene »

1000 wrote:
Mon Jul 26, 2021 5:16 am
I guess they'll have also create a name for the numbers in KiB, GiB , ...
If that's what you mean certainly those exist already: https://en.wikipedia.org/wiki/Binary_prefix. "kibibytes", "mebibytes", "gibibytes", etc. Latter being uncomfortably close to e.g. "gigglebytes" for some is part of the resistance to the change...
User avatar
Termy
Level 7
Level 7
Posts: 1787
Joined: Mon Sep 04, 2017 8:49 pm
Location: UK
Contact:

Re: bash script memory size in gb

Post by Termy »

Using AWK, the below one-liner might be what you want, to show the GiB (what you probably mean by GB) of the total RAM in your system. If you're just showing the total RAM, you probably don't need to actually get it (dynamic value), so could just use a static (unchanging) value.

Code: Select all

$ awk '$1 == "MemTotal:" {printf("%.2f GiB\n", $2 / 1024 / 1024)}' /proc/meminfo
13.61 GiB
If you're wanting the total RAM currently being used, you could instead use the below one-liner. Although you can figure it all out yourself — the calculation for which is explained in the free(1) man page — I've unfortunately had differing results to free(1).

Code: Select all

$ free -m | awk '$1 == "Mem:" {printf("%.2f GiB\n", $3 / 1024)}'
1.99 GiB
Here's a more manual approach, using the calculation mentioned above, but when I get results different (of about .10 GiB) to that which free(1) shows:

Code: Select all

$ awk '$1 == "MemTotal:" {Total = $2} $1 == "MemFree:" {Free = $2} $1 == "Buffers:" {Buffers = $2} $1 == "Cached:" {Cached = $2} END {printf("%.2f GiB\n", (Total - Free - Buffers - Cached) / 1024 / 1024)}' /proc/meminfo
2.16 GiB
In-case it's useful at all, here's a PERL equivalent to the above:

Code: Select all

$ perl -nae '$F[0] eq "MemTotal:" and $Total = $F[1]; $F[0] eq "MemFree:" and $Free = $F[1]; $F[0] eq "Buffers:" and $Buffers = $F[1]; $F[0] eq "Cached:" and $Cached = $F[1]; END {printf("%.2f GiB\n", ($Total - $Free - $Buffers - $Cached) / 1024 / 1024)}' /proc/meminfo
2.16 GiB
This has probably all been addressed already in this thread, but thought I'd chime in just in-case.
Confused? Try this guide to the Linux Mint Support Forums!

I'm Terminalforlife (LL) on YouTube and terminalforlife on GitHub.
rene
Level 17
Level 17
Posts: 7762
Joined: Sun Mar 27, 2016 6:58 pm

Re: bash script memory size in gb

Post by rene »

Termy wrote:
Mon Jul 26, 2021 6:56 am

Code: Select all

$ awk '$1 == "MemTotal:" {printf("%.2f GiB\n", $2 / 1024 / 1024)}' /proc/meminfo
13.61 GiB
The salient point here was more the rounding up (i.e., see above) but what I'm having a really hard time not commenting on is the general numerical mathematics point that one should do one's utmost to not e.g divide multiple times in order to not multiply inherent round-off/truncation errors. I.e., here it's better to use $2 / (1024 * 1024).

Pedantic, but too much of a red flag for someone in to that stuff...
User avatar
Termy
Level 7
Level 7
Posts: 1787
Joined: Mon Sep 04, 2017 8:49 pm
Location: UK
Contact:

Re: bash script memory size in gb

Post by Termy »

rene wrote:
Mon Jul 26, 2021 8:34 am
...
Yeah, it's weird maths, but it's fine to use in programming, at least in my experience — I do admit, your way is much nicer. I should probably do that more often, myself, but I suck at maths, and tend to lean towards the lazy 1024 / 1024 approach. :lol: How would you go up again, without using 1024 / 1024 / 1024?
Confused? Try this guide to the Linux Mint Support Forums!

I'm Terminalforlife (LL) on YouTube and terminalforlife on GitHub.
Post Reply