Why are JVM memory parameters usually in multiples of 256?
Asked Answered
O

5

9

I have seen almost all of the JVM memory parameters usually in multiples of 256 or a round binary value - e.g. 256m, 512m, 1024m etc. I know, it may be linked to the fact that physical memory (RAM) is usually a binary number, such as 256 MB, 1 GB etc.

My question is does it really help the memory management in anyway if JVM memory is set to a value that is a multiple of 256 or any binary value for that matter? Does it hurt to keep the JVM memory a round decimal value, such as 1000m, instead of 1024m - though I have never seen any JVM using such a value that is considered round in terms of decimal.

The OS would allocate the mentioned memory to JVM when it is launched, so I guess, it is more of a question for the JVM whether it can manage a round decimal memory size (e.g. 1000 MB) efficiently or would there be any shortcomings.

EDIT: I know, we CAN use decimal values for JVM memory, but my question is SHOULD we use decimal values?

EDIT2: For opinions/guesses about JVM being equally efficient of handling every memory size, please share any relevant links that you used for coming to that conclusion. I have seen enough WAR on this topic among fellow developers, but I haven't seen much of concrete reasoning to back either - decimal or binary value.

Occupational answered 12/8, 2014 at 10:8 Comment(2)
JIT makes your variables aligned properly so it doesnt matter i think.Orpiment
@Occupational it hardly matters, even if you give 1MB its in multiple of 2 (considering the bytes 1024 * 1024)Heller
G
8

It is not necessary to use a multiple of 2 for the JVM memory parameters. It is just common use for memory allocation to double the value if the old one isn't enough.

If you increase your assigned memory value in 1MB steps you will have to adjust the value several (hundred) times before the configuration matches you requirements. So it it just more comfortable to double the old value.

This relies on the fact that memory is a cheap ressource in those days.

EDIT:

As you already mentioned it is possible to assign values like 1000 MB or 381 MB. The JVM can handle every memory size that is big enough to host the permGenSpace, the stack and the heap.

Gaucherie answered 12/8, 2014 at 10:22 Comment(2)
Thanks. Yes, JMV CAN handle every memory size, but my question is does it waste any memory if I use a value which is not rounded to a multiple of 2? It is more of a best practice question, I guess. Is it stated anywhere in the specification or the implementation about how JVM manages the memory(some links to this info would be good)?Occupational
Due to the fact that the memory parameters only affect the minimum and maximum allocated memory for the JVM, they do not affect the actual allocated memory. This means that the JVM is allocating a value between min and max but it is up to JVM how many memory it allocates from the OS. If we take a look at the taskmanager or the resourcemonitor we will see that the memory allocated is always a multiple of 2 KB. For example 1,345,488 KB which is 1,313.95 MB. So the min allocation block size must be 2 or 4 KB.Gaucherie
S
3

It does not matter. There is no special treatment of rounded values.

You can specify the memory size within 1-byte accuracy - JVM itself will round the size up to the value it is comfortable with. E.g. the heap size is rounded to the 2 MB boundary. See my other answer: https://mcmap.net/q/131232/-why-is-output-of-maxheapsze-different-when-calling-xx-printflagsfinal-and-xx-printcommandlineflags

Sacristan answered 12/8, 2014 at 11:0 Comment(2)
Thanks for the answer. So it means that if you specify the memory which is not a multiple of 2, such as 123 MB, then it gets rounded to the nearest 2 MB boundary - i.e. 122 MB in this case. Essentially, the rounded 1 MB is wasted - is that right? Again, I saw the link that you posted was for OpenJDK, so do you know if it is part of the specification or is it left to the implementation to decide about such details?Occupational
@Occupational Heap sizes are usually rounded up, i.e. 123 -> 124 MB. Anyway, this is implementation specific.Sacristan
B
1

There is not real requirement those values be a multiplier of 2. It is just a way of use. you can use what ever values there.

-Xms1303m -Xmx2303m -XX:MaxPermSize=256m // my configs
Beau answered 12/8, 2014 at 10:10 Comment(1)
thanks. I know, we CAN use a decimal value. But do you have any memory profiling results or comparisons that show that it matters or not?Occupational
A
1

I think it is mainly a way of thinking, something like: I have a 1 GB memory, I will give JVM a half, which is 512 MB.

Ardene answered 12/8, 2014 at 11:26 Comment(0)
H
0

It is just a way to ensure the size that you specify (as argument) fits to the actual allocated memory, because the machine allocates the memory in blocks of size power of 2.

Heeled answered 12/8, 2014 at 10:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.