首页 > 对象占用大小问题

对象占用大小问题

我用Instrumentation的方式,使用它的getObjectSize输出对象大小,我的机器是64位,基于JDK 8,因此reference是8个字节,非数组对象应该是16个字节的对象头,输出也确实如此,但是数组也是16个字节的对象头,不是应该有一块用于记录数组长度的数据吗?

public static void main(String[] args) {
            //Object
            System.out.println(ClassUtils.sizeOf(new Object())); //16
            //数组
            System.out.println(ClassUtils.sizeOf(new byte[0])); //16
            System.out.println(ClassUtils.sizeOf(new byte[7])); //24 padding补齐
            System.out.println(ClassUtils.sizeOf(new byte[_1MB])); //1024 * 1024 + 16 = 1048592
        }

我在知乎的提问,R大的回答


这种比较细节的问题, 上知乎去问RFX
https://www.zhihu.com/people/rednaxelafx

【热门文章】
【热门文章】