JDK-8139457 : Relax alignment of array elements
  • Type: Enhancement
  • Component: hotspot
  • Sub-Component: runtime
  • Affected Version: 9,20,21,22
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • Submitted: 2015-10-12
  • Updated: 2024-04-25
  • Resolved: 2024-02-23
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 23
23 b12Fixed
Related Reports
CSR :  
Duplicate :  
Relates :  
Relates :  
Relates :  
Relates :  
Relates :  
Relates :  
Relates :  
Relates :  
Relates :  
Sub Tasks
JDK-8325944 :  
Description
If you allocate a "new byte[1]", then on 64-bit platform you will get this layout:

[B object internals:
 OFFSET  SIZE  TYPE DESCRIPTION
      0     4       (object header)
      4     4       (object header)
      8     4       (object header)
     12     4   int [B.length
     16     1  byte [B.<elements>
     17     7       (loss due to the next object alignment)
Instance size: 24 bytes (reported by Instrumentation API)
Space losses: 0 bytes internal + 7 bytes external = 7 bytes total

Everything is as expected: the elements start at offset 16, there is one element, and 7-byte external alignment shadow. However, the same test with -XX:-UseCompressedClassPointers:

[B object internals:
 OFFSET  SIZE  TYPE DESCRIPTION
      0     4       (object header)
      4     4       (object header)
      8     4       (object header)
     12     4       (object header)
     16     4   int [B.length
     20     4       (alignment/padding gap)
     24     1  byte [B.<elements>
     25     7       (loss due to the next object alignment)
Instance size: 32 bytes (reported by Instrumentation API)
Space losses: 4 bytes internal + 7 bytes external = 11 bytes total

Now, the elements are starting from offset 24! And there is a 4-byte gap between length field and elements. It seems to be because the starting offset for all arrays is granular to HeapWordSize:

arrayOop.hpp:

  // Returns the offset of the first element.
  static int base_offset_in_bytes(BasicType type) {
    return header_size(type) * HeapWordSize;
  }

But it seems to be little sense in aligning at least byte[] arrays by HeapWord? We can save quite some space on small arrays (when CCPs are disabled either explicitly, or implicitly due to large heap), if that gap is closed.

This is referenced in JOL Sample:
  http://hg.openjdk.java.net/code-tools/jol/file/c851de7dd320/jol-samples/src/main/java/org/openjdk/jol/samples/JOLSample_25_ArrayAlignment.java
Comments
Changeset: 336bbbe3 Author: Roman Kennke <rkennke@openjdk.org> Date: 2024-02-23 10:05:25 +0000 URL: https://git.openjdk.org/jdk/commit/336bbbe3895214a772e0f3aafb36277c46645ded
23-02-2024

[~rkennke] it was accidental during RT Triage, assigning back to you.
04-01-2023

@Lois: what is the reason for assigning this bug to you? If this was accidental, please assign it back to me. Thanks!
04-01-2023

Could we update the bug description to talk about UseCompressedClassPointers instead? It is not bound to compressed oops anymore.
05-12-2022

A pull request was submitted for review. URL: https://git.openjdk.org/jdk/pull/11044 Date: 2022-11-08 20:18:09 +0000
21-11-2022

I'm reopening this. It'll be important for Lilliput. I'm going to propose an implementation.
08-11-2022

If there is any interest, I could upstream this PR of Lilliput: https://github.com/openjdk/lilliput/pull/41 This would address the issue. It's arguably not very important currently, because +UseCompressedClassPointers is off by default. It is very important in Lilliput, because there the Klass* slides into the first 64bit of the header, and we would impose the 4 byte gap on *all* arrays.
26-04-2022

Not a priority, closing as WNF.
30-03-2017