Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Heap size: why use powers of 2? [duplicate]

Most often when I see examples on setting the Java heap size using -Xms and -xmx JVM parameters people use powers of two:

128m, 512m, 1024m, etc.

Is there actually a reason for this? Is this beneficial to the JVM performance in some way? Or is this simply done because programmers tend to like powers of 2? (Or because the default value is 64m?)

P.S. Of course I know that you can use essentially any number you want (-Xmx666). But people tend to use powers of 2.

like image 322
fgysin Avatar asked Sep 30 '22 04:09

fgysin


1 Answers

I can't tell why other people do it but for me, it's a) convention to give RAM sizes in powers of two since the memory modules use that and b) I know all the powers of two up to 65536 from memory, so it's not an extra effort to do it.

But from a OS / memory point of view, using powers of two for allocating megabytes doesn't have any impact. If we were talking about smaller sizes (like 511 or 512 bytes), then the memory management subsystem of the OS will round those values. But no OS rounds for megabytes.

like image 189
Aaron Digulla Avatar answered Oct 04 '22 20:10

Aaron Digulla