2

I have this code which displays the allocated JVM memory in (I suppose) bytes:

Runtime.getRuntime().totalMemory()

When I print the output I get something like 12232313131. Can you tell me how I can print this in megabytes and gigabytes?

Peter Penzov
  • 1,126
  • 134
  • 430
  • 808
  • This post may help you: http://stackoverflow.com/questions/3571203/what-is-the-exact-meaning-of-runtime-getruntime-totalmemory-and-freememory – Archer Sep 16 '13 at 14:07

1 Answers1

3

A nice trick is to use printf(...);

System.out.printf("%.3fGiB", Runtime.getRuntime().totalMemory() / (1024.0 * 1024.0 * 1024.0));
rolfl
  • 17,539
  • 7
  • 42
  • 76
  • Those figures are GiB not GB, google it – Vorsprung Sep 16 '13 at 13:55
  • I think that's a little more pedantic than the OP was expecting... I expect the OP was looking for GiB but still.... will edit post. – rolfl Sep 16 '13 at 13:56
  • sorry don't want to be pendantic and I know that some programs do GB=1024 ** 3 but the official standard is now GB is 1000 ** 3, might as well get it right :) – Vorsprung Sep 16 '13 at 14:01
  • 1
    @Vorsprung GiB is usually used for ram/memory, and usually when you see GB in references to memory, it is actually GiB. – nos Sep 16 '13 at 14:06
  • 1
    @nos I feel your pain but the IEEE have spoken http://physics.nist.gov/cuu/Units/binary.html – Vorsprung Sep 16 '13 at 14:14
  • Scientifically correct but total nonsense for non-scientific purposes. GB has been what GiB is for years. – zapl Sep 16 '13 at 14:15
  • @Vorsprung The page you linked to says that 1 GiB is 1024*1024*1024 , which is exactly what the answer here calculates. And it says that 1 GB is 1000*1000*1000. So all is well, apparently. – nos Sep 16 '13 at 14:19