sizeOf() calculation from ClassLayout seems to be wrong

Aleksey Shipilev aleksey.shipilev at oracle.com
Tue Jun 7 07:26:17 UTC 2016


Hi,

On 06/07/2016 03:47 AM, Enis Söztutar wrote:
> [B object internals:
> 
>  OFFSET  SIZE  TYPE DESCRIPTION                    VALUE
>       0     4       (object header)                01 00 00 00 (00000001
> 00000000 00000000 00000000) (1)
>       4     4       (object header)                00 00 00 00 (00000000
> 00000000 00000000 00000000) (0)
>       8     4       (object header)                f5 00 00 f8 (11110101
> 00000000 00000000 11111000) (-134217483)
>      12     4       (object header)                11 00 00 00 (00010001
> 00000000 00000000 00000000) (17)
>      16    17  byte [B.<elements>                  N/A
>      33     7       (loss due to the next object alignment)
> 
> Instance size: 40 bytes

This is a layout for byte[17] -- notice the length field which says
"17". And this is a correct layout.

> This little UT also fails with the latest source code base:
> 
> public class ArraySizeTest {
> 
>   @Test
>   public void testByteArraySize() {
>     System.err.println(VM.current().details()); // debug
> 
>     for (int i = 0; i < 100; i++) {
>       byte[] b = new byte[i];
>       long vmSize = VM.current().sizeOf(i);

This calculates the size of boxed Integer "i", not byte[] "b" ;) For
sure, it is 16 bytes always. If you sizeOf(b), then the test passes on
all i-s in [0, 100000].

>       long clSize = ClassLayout.parseInstance(b).instanceSize();
>       Assert.assertEquals("sizeOf() from VM and ClassLayout does not match,
> VM.sizeOf()="
>         + vmSize + ", ClassLayout.instanceSize()=" + clSize, vmSize, clSize
> );
>     }
>   }
> }

Thanks,
-Aleksey



More information about the jol-dev mailing list