JVM-对象

打印对象的内存布局

    /**
     * 打印对象信息
     */
    @Test
    public void printObj() {
        User user = new User();
        System.out.println("hashCode:"+user.hashCode());
        System.out.println(ClassLayout.parseInstance(user).toPrintable());
    }

    private static class User{
    }
com.txp.owner.basic.record.test202302.TestMain20230208$User object internals:
 OFFSET  SIZE   TYPE DESCRIPTION                               VALUE
      0     4        (object header)                           01 3a 99 d6 (00000001 00111010 10011001 11010110) (-694601215)
      4     4        (object header)                           1e 00 00 00 (00011110 00000000 00000000 00000000) (30)
      8     4        (object header)                           07 26 01 f8 (00000111 00100110 00000001 11111000) (-134142457)
     12     4        (loss due to the next object alignment)
Instance size: 16 bytes
Space losses: 0 bytes internal + 4 bytes external = 4 bytes total

对象头一定是8的倍数,前8个字节存储的是mark world信息,后面8个字节存储的是类型指针。
因为开启了指针压缩,第2个8个字节实际只占用了4个字节。
对象=对象头+实例数据+padding填充字节组成。
对象头=mark world(8个字节)+类型指针(8个字节)
参考:
https://mp.weixin.qq.com/s/pF0ursDX6-LD1h5LSSAttw

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值