java对象布局

<dependencies>
    <dependency>
        <groupId>org.openjdk.jol</groupId>
        <artifactId>jol-core</artifactId>
        <version>0.9</version>
    </dependency>
</dependencies>

class User{
    boolean isUser = false;
    int i = 1;
    public void test(){
        System.out.printf("test");
    }
}

public class ObjectHeadTest {
    static User user = new User();
    public static void main(String[] args) {
        System.out.println("hashCode:" + user.hashCode());
        //输出对象布局,即对象在堆中存储内容。
        System.out.println(ClassLayout.parseInstance(user).toPrintable());
    }
}

输出:
hashCode:21685669
com.maindemo.concurrent.User object internals:
 OFFSET  SIZE      TYPE DESCRIPTION                               VALUE
      0     4           (object header)                           01 a5 e5 4a (00000001 10100101 11100101 01001010) (1256563969)
      4     4           (object header)                           01 00 00 00 (00000001 00000000 00000000 00000000) (1)
      8     4           (object header)                           43 c1 00 20 (01000011 11000001 00000000 00100000) (536920387)
     12     4       int User.i                                    1
     16     1   boolean User.isUser                               false
     17     7           (loss due to the next object alignment)

4,5,6行是对象头(object header),其中4,5行8个字节存的是mark word,第6行8个字节存的是klass pointer。
第7行是4个字节的int i = 1;
第8行是1个字节的boolean isUser = false;
第9行是补数据,对象字节数必须是8的倍数。

mark word里面共有00 00 00 01 4a e5 a5 01,8个字节,64位,当前对象锁状态为无锁,此时每个位的含义如下:
25位无意义 | 31位存的是hashCode(十六进制01 4a e5 a5 = 十进制21685669) | 1位无意义 | 4位存的是对象的年龄(在年轻代每回收一次还存活则加1) | 1位存的是偏向锁状态 | 2位存的是锁状态
最后3位结合表示了5种状态:无锁,偏向锁,轻量锁,重量锁,被gc标志(代表该对象可回收)
官方说明文档:
http://openjdk.java.net/groups/hotspot/docs/HotSpotGlossary.html

object header
Common structure at the beginning of every GC-managed heap object. (Every oop points to an object header.)
Includes fundamental information about the heap object's layout, type, GC state, synchronization state, and identity hash code.
Consists of two words. In arrays it is immediately followed by a length field.
Note that both Java objects and VM-internal objects have a common object header format.
对象头包含对象的结构,类型,GC状态,锁状态和hashcode。

mark word
The first word of every object header. Usually a set of bitfields including synchronization state and identity hash code.
May also be a pointer (with characteristic low bit encoding) to synchronization related information. During GC, may contain GC state bits.
对象头第一个word是mark word,里面包含锁状态和hashcode。

klass pointer
The second word of every object header. Points to another object (a metaobject) which describes the layout and behavior of the original object.
For Java objects, the "klass" contains a C++ style "vtable".
对象头第二个word是klass pointer,它存储的是类元信息的内存地址。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值