Profile Image
vishnupriya

What is Java Object Alignment: -XX:ObjectAlignmentInBytes?

What is Java Object Alignment: -XX:ObjectAlignmentInBytes? Have you used this JVM argument before? What are the pros & cons of using this Java argument? Can you share your perspective/experience in using this JVM argument?

  • jvm-argument

  • xx-objectalignmentinbytes

  • x-objectalignmentinbytes

  • Java Object Alignment

Please Sign In or to post your comment or answer

Profile Image

Pavel Khodakovsky

Usage:

• -XX:ObjectAlignmentInBytes= where alignment is a number that must be a power of 2 between 8 and 256 inclusive

 

Example:

java -Xmx128g -XX:ObjectAlignmentInBytes=32 -XX:+UseCompressedOops

 

Description:

 

The -XX:ObjectAlignmentInBytes option sets the memory alignment of Java objects in bytes. If this value is N then the Java heap is laid out such that the start of each object is a multiple of N bytes after the start of the previous object.

 

For example suppose the object alignment is 8 bytes, which is the default. If object A starts at memory location 1024 and occupies 5 bytes, then there will be 3 bytes of padding following it. Object B, the next object in memory, will start at location 1032. Supposing that Object B occupies 10 bytes then it will be followed by 6 bytes of padding (to reach 16, the next multiple of 8). The next object in memory after B would start at location 1048. The layout of these objects in memory would be as follows:

 

1024 +----------------+
| |
| |
| |
| Object A |
| (5 bytes) |
| |
| |
| |
+----------------+
| |
| Padding |
| 3 bytes |
| |
1032 +----------------+
| |
| |
| |
| |
| |
| |
| |
| |

| Object B |
| (10 bytes) |
| |
| |
| |
| |
| |
1040 | |
| |
| |
+----------------+
| |
| |
| |
| |
| Padding |
| 6 bytes |
| |
| |
| |
| |
1048 +----------------+
| Object C |
| (2 bytes) |
+----------------+

 

The -XX:ObjectAlignmentInBytes option must be specified as a power of 2
and must be in the range 8 to 256 inclusive. This means only the following
values are permitted:


• 8
• 16
• 32
• 64
• 128
• 256

 

By setting -XX:ObjectAlignmentInBytes to a value higher than 8, compressed
object pointers can be used to address heap sizes greater than 32 GB. This is
because the value of the compressed pointer is multiplied by the object alignment
byte count in order to find the physical memory address of the start of an object.


However even with a larger alignment size, a compressed 32-bit pointer can
only address a maximum of 4,294,967,296 distinct objects. Also when the
alignment size is large, the unused space between objects increases, which can
waste physical memory.

Default Value:


The default value of -XX:ObjectAlignmentInBytes is 8 bytes, i.e. objects are
laid out in memory such that the start of each object is a multiple of 8 bytes
after the start of the previous object.

 

Errors:


If the value of this parameter is not a power of 2, or is outside the range 8 to
256, then an error is printed and the JVM exits.


For example:


java -XX:ObjectAlignmentInBytes=9


results in:


ObjectAlignmentInBytes (9) must be power of 2
Improperly specified VM option 'ObjectAlignmentInBytes=9'
Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.


and


java -XX:ObjectAlignmentInBytes=512


results in:


intx ObjectAlignmentInBytes=512 is outside the allowed range [ 8 ... 256 ]
Improperly specified VM option 'ObjectAlignmentInBytes=512'
Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.

 

Arguments Related to -XX:-ObjectAlignmentInBytes:


• TODO: link to -Xmx
• TODO: link to -XX:-UseCompressedOops
• TODO: link to -XX:-UseCompressedClassPointers

 

Related Posts:


• Compressed OOPs in the JVM


NOTE:


If you have additional comments, interesting experiences or even point of disagreement with this JVM argument description, please leave a comment. Your
insights will help the entire 10+ million java developer community to develop
one standard source of documentation for all the JVM arguments.

Got something else on mind? Post Your Question

Not the answer you're looking for? Browse other questions tagged
  • jvm-argument

  • xx-objectalignmentinbytes

  • x-objectalignmentinbytes

  • Java Object Alignment