7

Hello everybody, I have observed while setting heap size people prefer the values 64,128,256,1024.. . If I give a value in- between these numbers (say 500), won't jvm accept that value ? Why these numbers are important and preferred ? Why we also upgrade RAM in this pattern.

Please help me to understand.

Thanks in advance, Habin

Habin
  • 762
  • 1
  • 11
  • 21

5 Answers5

7

JVM will accept any value, no problem with that. Using 2^n values is just a "convention", using others will have no negative effect in practice.

Alexey A.
  • 1,389
  • 1
  • 11
  • 20
2

Well, if you think about it this way:

  • 1 byte is 8 bits

  • 1 kb = 1024 bytes

  • 1 mb = 1024 kb

  • 1 gb = 1024 mb

  • ... and so on ...

It's not just 2^n. Things in terms of memory in computing are closely related to the number eight - the number which defines one byte in most modern computers.

The main reason why bits are grouped together is to represent characters. Because of the binary nature of all things computing, ideal 'clumps' of bits come in powers of 2 i.e. 1, 2, 4, 8, 16, 32.... (basically because they can always be divided into smaller equal packages (it also creates shortcuts for storing size, but that's another story)). Obviously 4 bits (nybble in some circles) can give us 2^4 or 16 unique characters. As most alphabets are larger than this, 2^8 (or 256 characters) is a more suitable choice.

Machines exist that have used other length bytes (particularly 7 or 9). This has not really survived mainly because they are not as easy to manipulate. You certainly cannot split an odd number in half, which means if you were to divide bytes, you would have to keep track of the length of the bitstring.

Finally, 8 is also a convenient number, many people (psychologists and the like) claim that the human mind can generally recall only 7-8 things immediately (without playing memory tricks).

carlspring
  • 27,224
  • 23
  • 101
  • 178
  • This answer is focusing on the ideal byte size for a machine, but I don't see what connection it has to the JVM's heap size (which is a multiple of bytes) – Alexander Apr 22 '21 at 14:33
0

If it won't accept the value, check whether you put a megabytes(M or m) or gigabytes(G or g) modifier after the amount.

Example: java -Xms500M -Xmx500M -jar myJavaProgram.jar

Also, take a look at this link.

ioreskovic
  • 5,122
  • 5
  • 34
  • 62
0

Why we also upgrade RAM in this pattern.

That is because memory chips / cards / come in sizes that are a power of 2 bytes. And the fundamental reason for that is that it makes the electronics simpler. And simpler means cheaper, more reliable and (probably) faster.

Stephen C
  • 632,615
  • 86
  • 730
  • 1,096
  • As far as I understand, memory sizes come in powers of two because of the memory bus sizes. I'll use an example with small/easy numbers. Imagine you had a machine with `128` bytes of memory (a memory space ranging `0b0000_0000` to `0b0111_1111`) that you wanted to upgrade to `192` bytes (which requires the new ability to address `0b0111_1111` - `0b1011_1111`). Doing that would require expanding the memory bus from 7 pins to 8 pins. But at that point, you get the ability to address 256 bytes (max addr. `0b1111_1111`), half of which you would be wasting. – Alexander Apr 22 '21 at 14:31
  • If that's correct, then I don't understand what it has to do with the JVM's sizes – Alexander Apr 22 '21 at 14:31
  • Nowhere in my answer am I talking about JVM sizes. I am just answering the part of the question that asks about RAM sizes. – Stephen C Apr 22 '21 at 14:33
  • Oh true, I guess I was reading into it because of the entirety of the question. Makes sense. Was my understanding in the first comment correct, though? – Alexander Apr 22 '21 at 14:33
  • 1
    Yes. It is correct. – Stephen C Apr 22 '21 at 14:34
0

Except non-written convention, it has also performance impact - depending on the the architecture of the machine.

For example if a machine is ternary based, it would work better with a heap size set to a value which is a power of 3.

vtor
  • 7,893
  • 3
  • 45
  • 65
  • "it would work better with a heap size set to a value which is a power of 3. " Why would that be? And if that were true, wouldn't it also be true that a binary machine would work with a value which is a power of two? – Alexander Apr 22 '21 at 14:23