Score:0

Convert byte value to MB in bash script

nf flag

I have written a bash script to get the current Jjava heap memory size. I want to convert this value to MB. Below are the details

freememory=$((heapmax - heapused))
echo $freememory
Remaining Memory is : $freememory bytes"
24601 avatar
in flag
how does this relate to ask ubuntu?
Hannu avatar
ca flag
Hmm? Ubuntu does not have free memory? ;-) *Sorry! Could not resist!*
Score:0
hr flag

If you want to convert to "human readable" units (like the -h switch used by utilities like df, du etc.) then try numfmt ex.

$ echo 123456789 | numfmt --to=iec
118M

$ echo 123456789 | numfmt --to=iec-i
118Mi

$ echo 123456789 | numfmt --to=si
124M
Score:0
om flag

This is typically something you can use bc for.

echo "${freememory}/2^20" | bc
Score:0
ca flag

Attempting a single line of Python code to generate kilo to terra-byte values:

$ python -c "import sys;from math import *;a=float(sys.argv[1]);    \  
  m=int(round(log(a)/log(1000),3));                                 \
  print(a,'is',a/(pow(1000,m)),(' KMGT')[int(m)]+'B','free mem')"   \
  $(sed -nr 's/^.*MemFree:[ \t\n]+([0-9]+).*$/\1000/p' /proc/meminfo)

... actually, just append characters to KMGT to allow even larger quantities.

Lines:

  1. launch python, import math and sys, get first arg into a
  2. calculate the magnitude of a
  3. print the result
  4. create the first argument, what is to be in sys.argv[1]
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.