Java对象

对象在堆中的存储布局

它保存了什么



对象指向它的类元数据的指针,虚拟机通过这个指针来确定这个对象是哪个类的实例
对象头有多大?在64位系统中,Mark Word占了8个字节,类型指针占了8个字节,一共是16个字节。

对象头+实例数据+对齐填充的演示说明

64位Mark Word对象头

源码定义


代码演示
<dependency>
<groupId>org.openjdk.jol</groupId>
<artifactId>jol-core</artifactId>
<version>0.9</version>
</dependency>
查看VM信息
public class JOLDemo {
public static void main(String[] args) {
//Thread.currentThread()
System.out.println(VM.current().details());
}
}

查看对象头的信息
public static void main(String[] args) {
Object o = new Object();
System.out.println(ClassLayout.parseInstance(o).toPrintable());
}

说明

加入属性后
public class JOLDemo {
public static void main(String[] args) {
Object o = new Object();
// System.out.println(ClassLayout.parseInstance(o).toPrintable());
Customer customer = new Customer();
System.out.println(ClassLayout.parseInstance(customer).toPrintable());
}
}
class Customer{
long id;
int age;
}

对象分代年龄 4个1为15

类型指针为4个字节?? 开启了压缩参数

JVM内存布局


















