30

It is commonly known that it is possible to limit the Java heap size with -Xmx<amount><unit>, where unit is the data amount unit like Gigabyte, Megabyte, etc. I know that -Xmx128M means 128 Mebibytes (= 128 * 1024 * 1024 bytes).

But is it true, that it is also possible to use decimal units like megabytes using -Xmx100m (which would be 100 * 1000 * 1000 bytes)?

So is it possible to use this decimal units by using lower-case unit suffixes like k, m, g instead of K, M, G?

trincot
  • 317,000
  • 35
  • 244
  • 286
MinecraftShamrock
  • 3,504
  • 2
  • 25
  • 44
  • 3
    BTW The case generally matters, it's just that javac doesn't care. Note: there is no need to wonder whether g/G means 100^3 or 1024^3 because it won't be exactly either. ;) On my system running `System.out.println(Runtime.getRuntime().maxMemory());` with `-Xmx1g` prints `954728448` – Peter Lawrey May 16 '14 at 17:11
  • @PeterLawrey That's interesting to know. – MinecraftShamrock May 16 '14 at 17:22

3 Answers3

41

There is no difference between k and K both means kibibyte and so does m/M = mebibyte & g/G = gibibyte. you can set 100m as the value and it will be 100 * 1024 * 1024 bytes. generally it is advised to use this value in powers of 2.

Hope it helps.

Cody A. Ray
  • 5,869
  • 1
  • 37
  • 31
Sanjeev
  • 9,876
  • 2
  • 22
  • 33
  • 4
    *why* is it advisable to use in powers of two ? I would question that. If you want to add 1g to an 8g server, that should be no issue. – Brian Agnew Apr 07 '20 at 13:52
5

Why don't you just try -Xmx100m and -Xmx100M and check if there is any difference.
k, m, g work exactly like K, M, G - they all mean binary units.

apangin
  • 92,924
  • 10
  • 193
  • 247
2

All memory sizes used by the JVM command line arguments are specified in binary units (Kibibyte, Mebibyte, etc.) regardless of unit capitalization; where a 'kilobyte' is 1024 bytes, and the others are increasing powers of 1024.

This answer goes into a more complete technical answer to the question.

Community
  • 1
  • 1
TerekC
  • 2,133
  • 3
  • 20
  • 22