Java Top

I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2:

>> CHECK OUT THE COURSE

1. Overview

In this quick tutorial, we're going to see how and where the HotSpot JVM stores the array length.

Usually, the memory layout of run-time data areas is not part of the JVM specification and is left to the discretion of the implementor. Therefore, each JVM implementation may have a different strategy to layout objects and arrays in memory.

In this tutorial, we're focusing on one specific JVM implementation: the HotSpot JVM. We also may use the JVM and HotSpot JVM terms interchangeably.

2. Dependency

In order to inspect the memory layout of arrays in the JVM, we're going to use the Java Object Layout (JOL) tool. Therefore, we need to add the  jol-core dependency:

<dependency> 
    <groupId>org.openjdk.jol</groupId> 
    <artifactId>jol-core</artifactId>    
    <version>0.10</version> 
</dependency>

3. Array Length

The HotSpot JVM uses a data structure called Ordinary Object Pointers (OOPs) to represent pointers to objects. To be more specific, the HotSpot JVM represents the arrays with a special OOP called arrayOop. Each arrayOop includes an object header with the following details:

  • One mark word to store the identity hash code or GC information
  • One klass word to store general class metadata
  • 4 bytes representing the array length

Therefore, the JVM stores the array length in the object header.

Let's verify this by inspecting the memory layout of an array:

int[] ints = new int[42];
System.out.println(ClassLayout.parseInstance(ints).toPrintable());

As shown above, we're parsing the memory layout from an existing array instance. Here's how the JVM lays out the int[]:

[I object internals:
 OFFSET  SIZE   TYPE DESCRIPTION               VALUE
      0     4        (object header)           01 00 00 00 (00000001 00000000 00000000 00000000) (1) # mark
      4     4        (object header)           00 00 00 00 (00000000 00000000 00000000 00000000) (0) # mark
      8     4        (object header)           6d 01 00 f8 (01101101 00000001 00000000 11111000) (-134217363) #klass
     12     4        (object header)           2a 00 00 00 (00101010 00000000 00000000 00000000) (42) # array length
     16   168    int [I.<elements>             N/A
Instance size: 184 bytes

As mentioned earlier, the JVM stores the array length inside the object header after mark and klass words. Also, the array length will be stored in 4 bytes, so it can't be greater than the maximum value for a 32-bit integer.

After the object header, the JVM stores the actual array elements. Since we have an array of 42 integers, the total size of the array is 168 bytes — 42 multiplied by 4.

4. Conclusion

In this short tutorial, we saw how the JVM stores the array length.

As usual, all the examples are available over on GitHub.

Java bottom

I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2:

>> CHECK OUT THE COURSE
Comments are closed on this article!