bash script memory size in gb

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
rene
Level 20
Level 20
Posts: 12212
Joined: Sun Mar 27, 2016 6:58 pm

Re: bash script memory size in gb

Post by rene »

Termy wrote: Mon Jul 26, 2021 9:54 am How would you go up again, without using 1024 / 1024 / 1024?
Not sure I understand what you mean; n / a / b / c is simply n / (a * b * c); in this special case, n / 1024^3.
User avatar
Termy
Level 12
Level 12
Posts: 4248
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 10:11 am ...
Yep, you answered the question exactly. Thanks! :) I don't fully understand it, because I suck at maths, but I have a basic understanding of it.

Thanks to your reply, I now understand that I can do this:

Code: Select all

free -b | awk '$1 == "Mem:" {printf("%.2f GiB\n", $3 / (1024 ^ 3))}'
Instead of this:

Code: Select all

free -b | awk '$1 == "Mem:" {printf("%.2f GiB\n", $3 / 1024 / 1024 / 1024)}'
To get the GiB from bytes.
I'm also Terminalforlife on GitHub.
rene
Level 20
Level 20
Posts: 12212
Joined: Sun Mar 27, 2016 6:58 pm

Re: bash script memory size in gb

Post by rene »

Yes. And just to reinforce my earlier reply then: note how e.g. 1024^3 has a precise (integer) representation also in the computer; when you divide by it you still introduce a potential round-off error inherent to floating point math --- but only once. If you do N / 1024 / 1024 / 1024 you have three such errors: "error upon error" which may in a manner similar to "interest upon interest" blow up significantly in the end if you're not quite careful.

As said, it felt a bit pedantic to point out, but when you do these things routinely that kind of thing jumps out at you. It's not a major problem in this specific instance but generally one should always strive to keep lossy operations such as divide to a minimum.
User avatar
Termy
Level 12
Level 12
Posts: 4248
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 12:17 pm ...
I had absolutely no idea that was a thing. :shock: I've never had any errors — that is — I've never had an incorrect number when dividing, as far as I know, but I only tend to use N.NNN at most, so I'm guessing this is only really a problem with numbers like N.NNNNNN, where such specificity is needed; is that right? What confuses me, now, is that you're still dividing by the (N ^ N) value, so don't you still wind up with the chance for an error?
I'm also Terminalforlife on GitHub.
rene
Level 20
Level 20
Posts: 12212
Joined: Sun Mar 27, 2016 6:58 pm

Re: bash script memory size in gb

Post by rene »

A very big thing even; it's the subject of numerical analysis. Yes, not only do you "wind up with the chance for an error", given necessarily finite-length computer representation of numbers said error potential is inherent to computer math: 1/3 or Pi, say, is never going to be exactly representable numerically in a computer.

As also said above though the thing is to as far as possible not amplify errors by chaining potential error producing operations one after the other. I.e., if you divide once by in this case 1024 * 1024 * 1024 you have only the one potential round-off/truncation whereas if you divide three times by 1024 you have that same issue three times.

In this case we were talking about awk and it in fact internally uses nice, large precision arithmetic so it's certainly here quite theoretical; given however that you replied to an already solved thread, i.e., in a general or tutorial-y sense, I still felt it wanted to be remarked upon.
User avatar
Termy
Level 12
Level 12
Posts: 4248
Joined: Mon Sep 04, 2017 8:49 pm
Location: UK
Contact:

Re: bash script memory size in gb

Post by Termy »

rene wrote: Tue Jul 27, 2021 9:22 am ...
If you have a YouTube channel talking about all this stuff, I'd happily watch it! :) Thanks for the information.
I'm also Terminalforlife on GitHub.
rene
Level 20
Level 20
Posts: 12212
Joined: Sun Mar 27, 2016 6:58 pm

Re: bash script memory size in gb

Post by rene »

Termy wrote: Tue Jul 27, 2021 11:33 am If you have a YouTube channel talking about all this stuff, I'd happily watch it! :)
I feel it safe to say that there's few things which I have more not.
FreedomTruth
Level 4
Level 4
Posts: 443
Joined: Fri Sep 23, 2016 10:19 am

Re: bash script memory size in gb

Post by FreedomTruth »

Kadaitcha Man wrote: Thu Jul 22, 2021 3:12 am
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
Haha :lol: That looks similar to some "random number generator" code I once saw. :D
User avatar
Termy
Level 12
Level 12
Posts: 4248
Joined: Mon Sep 04, 2017 8:49 pm
Location: UK
Contact:

Re: bash script memory size in gb

Post by Termy »

FreedomTruth wrote: Thu Jul 29, 2021 11:04 am Haha :lol: That looks similar to some "random number generator" code I once saw. :D
I'm guessing it was something along these lines:

Code: Select all

printf '%04d\n' $((RANDOM % 9999))
Which generates a random zero-padded number between 0 and 9999.
I'm also Terminalforlife on GitHub.
Locked

Return to “Scripts & Bash”