8

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 the JVM accept that value? Why these numbers are important and preferred? Why we also upgrade RAM in this pattern?

Please help me to understand.

3
  • May be because of people prefer to have their memory modules on-board full of JVM :) Commented Jul 13, 2012 at 7:10
  • 1
    Round numbers are pretty. As far as RAM upgrades, try finding a 743 MB module. Commented Jul 13, 2012 at 7:32
  • The answers here make statements that could use a little more proof. For lack of proof the accepted answer is probably our safest bet. But I'd love some support added to the other answers. Commented Feb 11, 2020 at 1:33

5 Answers 5

9

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.

Sign up to request clarification or add additional context in comments.

Comments

3

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).

1 Comment

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)
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.

Comments

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.

4 Comments

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.
If that's correct, then I don't understand what it has to do with the JVM's sizes
Nowhere in my answer am I talking about JVM sizes. I am just answering the part of the question that asks about RAM sizes.
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?
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.

1 Comment

"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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.