Bug in "loss due alignment" report?

Jaromir Hamala jaromir.hamala at gmail.com
Tue Oct 13 08:14:06 UTC 2015


Hi Aleksey,

the `parseInstance()`works nicely, thanks! However the old code is now
throwing NPE:

public static void main(String[] args) {
        int size = 1;
        byte[] b = new byte[size];

        out.println(ClassLayout.parseClass(b.getClass()).toPrintable(b));
    }


Exception in thread "main" java.lang.NullPointerException
at org.openjdk.jol.info.ClassData.arrayLength(ClassData.java:68)
at org.openjdk.jol.info.ClassData.parseArray(ClassData.java:89)
at org.openjdk.jol.info.ClassData.parse(ClassData.java:95)
at org.openjdk.jol.info.ClassData.parseClass(ClassData.java:64)
at org.openjdk.jol.info.ClassLayout.parseClass(ClassLayout.java:66)
at org.openjdk.jol.info.ClassLayout.parseClass(ClassLayout.java:50)
at
org.openjdk.jol.samples.JOLSample_01_Basic.main(JOLSample_01_Basic.java:58)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)

Cheers,
Jaromir



On Mon, Oct 12, 2015 at 10:32 PM, Aleksey Shipilev <
aleksey.shipilev at oracle.com> wrote:

> Hi Jaromir,
>
> On 10/10/2015 08:12 PM, Jaromir Hamala wrote:
> > The alignment waste should be 7 and not 8 bytes and the element size
> should
> > be 1. I realize it can be tricky to fix as the class parser does not have
> > an access to the actual object instance, but the current report is rather
> > misleading.
>
> Yes, I agree.
>
> And the reason you provided is exactly what is happening. Without
> changing the class parser to accept the instance itself, it would be
> hard to accommodate any variable-length instance.
>
> Luckily, this is easy enough to fix. See:
>  https://bugs.openjdk.java.net/browse/CODETOOLS-7901530
>
> Notably, see a new sample:
>
> http://hg.openjdk.java.net/code-tools/jol/file/tip/jol-samples/src/main/java/org/openjdk/jol/samples/JOLSample_25_ArrayAlignment.java
>
> Please test!
>
> Thanks,
> -Aleksey
>
>


-- 
“Perfection is achieved, not when there is nothing more to add, but when
there is nothing left to take away.”
Antoine de Saint Exupéry


More information about the jol-dev mailing list