欢迎关注b站账号/公众号【六边形战士夏宁】,一个要把各项指标拉满的男人。该文章已在github目录收录。
屏幕前的大帅比和大漂亮如果有帮助到你的话请顺手点个赞、加个收藏这对我真的很重要。别下次一定了,都不关注上哪下次一定。
当Java虚拟机遇到一条字节码new指令时,首先将去检查这个指令的参数是否能在常量池中定位到 一个类的符号引用,并且检查这个符号引用代表的类是否已被加载、解析和初始化过。如果没有,那必须先执行相应的类加载过程。
在HotSpot虚拟机里,对象在堆内存中的存储布局可以划分为三个部分:对象头(Header)、实例数据(Instance Data)和对齐填充(Padding)。
也可以使用jvisualvm(高版本jdkbin目录下)
代码如下
public static void main(String[] args) { ClassLayout layout = ClassLayout.parseInstance(new Object()); System.out.println("------------------layout------------------"); System.out.println(layout.toPrintable()); ClassLayout layout1 = ClassLayout.parseInstance(new Parent()); System.out.println("------------------layout1------------------"); System.out.println(layout1.toPrintable()); Parent parent = new Parent().setChild(new Child()); ClassLayout layout2 = ClassLayout.parseInstance(parent); System.out.println("------------------layout2------------------"); System.out.println(layout2.toPrintable()); ClassLayout layout3 = ClassLayout.parseInstance(new Child()); System.out.println("------------------layout3------------------"); System.out.println(layout3.toPrintable()); char c1 = '1'; ClassLayout layout4 = ClassLayout.parseInstance(c1); System.out.println("------------------layout4------------------"); System.out.println(layout4.toPrintable()); char c2 = '你'; ClassLayout layout5 = ClassLayout.parseInstance(c2); System.out.println("------------------layout5------------------"); System.out.println(layout5.toPrintable()); char[] arrC1 = new char[0]; System.out.println("------------------layout6------------------"); ClassLayout layout6 = ClassLayout.parseInstance(arrC1); System.out.println(layout6.toPrintable()); char[] arrC2 = new char[]{'你','好'}; ClassLayout layout7 = ClassLayout.parseInstance(arrC2); System.out.println("------------------layout7------------------"); System.out.println(layout7.toPrintable()); char[] arrC3 = new char[]{'你'}; ClassLayout layout8 = ClassLayout.parseInstance(arrC3); System.out.println("------------------layout8------------------"); System.out.println(layout8.toPrintable()); while (true){ parent.toString(); } } class Child { private List<String> list; private String str; } class Parent { private Child child; }
引用jar包
<dependency> <groupId>org.openjdk.jol</groupId> <artifactId>jol-core</artifactId> <version>0.9</version> </dependency>
执行结果如下:
------------------layout------------------ java.lang.Object object internals: OFFSET SIZE TYPE DESCRIPTION VALUE 0 4 (object header) 01 00 00 00 (00000001 00000000 00000000 00000000) (1) 4 4 (object header) 00 00 00 00 (00000000 00000000 00000000 00000000) (0) 8 4 (object header) e5 01 00 f8 (11100101 00000001 00000000 11111000) (-134217243) 12 4 (loss due to the next object alignment) Instance size: 16 bytes Space losses: 0 bytes internal + 4 bytes external = 4 bytes total ------------------layout1------------------ com.example.demo.lesson.grace.optional.Parent object internals: OFFSET SIZE TYPE DESCRIPTION VALUE 0 4 (object header) 05 00 00 00 (00000101 00000000 00000000 00000000) (5) 4 4 (object header) 00 00 00 00 (00000000 00000000 00000000 00000000) (0) 8 4 (object header) c4 f3 00 f8 (11000100 11110011 00000000 11111000) (-134155324) 12 4 com.example.demo.lesson.grace.optional.Child Parent.child null Instance size: 16 bytes Space losses: 0 bytes internal + 0 bytes external = 0 bytes total ------------------layout2------------------ com.example.demo.lesson.grace.optional.Parent object internals: OFFSET SIZE TYPE DESCRIPTION VALUE 0 4 (object header) 05 00 00 00 (00000101 00000000 00000000 00000000) (5) 4 4 (object header) 00 00 00 00 (00000000 00000000 00000000 00000000) (0) 8 4 (object header) c4 f3 00 f8 (11000100 11110011 00000000 11111000) (-134155324) 12 4 com.example.demo.lesson.grace.optional.Child Parent.child (object) Instance size: 16 bytes Space losses: 0 bytes internal + 0 bytes external = 0 bytes total ------------------layout3------------------ com.example.demo.lesson.grace.optional.Child object internals: OFFSET SIZE TYPE DESCRIPTION VALUE 0 4 (object header) 05 00 00 00 (00000101 00000000 00000000 00000000) (5) 4 4 (object header) 00 00 00 00 (00000000 00000000 00000000 00000000) (0) 8 4 (object header) 06 f4 00 f8 (00000110 11110100 00000000 11111000) (-134155258) 12 4 java.util.List Child.list null 16 4 java.lang.String Child.str null 20 4 (loss due to the next object alignment) Instance size: 24 bytes Space losses: 0 bytes internal + 4 bytes external = 4 bytes total ------------------layout4------------------ java.lang.Character object internals: OFFSET SIZE TYPE DESCRIPTION VALUE 0 4 (object header) 05 00 00 00 (00000101 00000000 00000000 00000000) (5) 4 4 (object header) 00 00 00 00 (00000000 00000000 00000000 00000000) (0) 8 4 (object header) c6 20 00 f8 (11000110 00100000 00000000 11111000) (-134209338) 12 2 char Character.value 1 14 2 (loss due to the next object alignment) Instance size: 16 bytes Space losses: 0 bytes internal + 2 bytes external = 2 bytes total ------------------layout5------------------ java.lang.Character object internals: OFFSET SIZE TYPE DESCRIPTION VALUE 0 4 (object header) 05 00 00 00 (00000101 00000000 00000000 00000000) (5) 4 4 (object header) 00 00 00 00 (00000000 00000000 00000000 00000000) (0) 8 4 (object header) c6 20 00 f8 (11000110 00100000 00000000 11111000) (-134209338) 12 2 char Character.value 你 14 2 (loss due to the next object alignment) Instance size: 16 bytes Space losses: 0 bytes internal + 2 bytes external = 2 bytes total ------------------layout6------------------ [C object internals: OFFSET SIZE TYPE DESCRIPTION VALUE 0 4 (object header) 01 00 00 00 (00000001 00000000 00000000 00000000) (1) 4 4 (object header) 00 00 00 00 (00000000 00000000 00000000 00000000) (0) 8 4 (object header) 41 00 00 f8 (01000001 00000000 00000000 11111000) (-134217663) 12 4 (object header) 00 00 00 00 (00000000 00000000 00000000 00000000) (0) 16 0 char [C.<elements> N/A Instance size: 16 bytes Space losses: 0 bytes internal + 0 bytes external = 0 bytes total ------------------layout7------------------ [C object internals: OFFSET SIZE TYPE DESCRIPTION VALUE 0 4 (object header) 01 00 00 00 (00000001 00000000 00000000 00000000) (1) 4 4 (object header) 00 00 00 00 (00000000 00000000 00000000 00000000) (0) 8 4 (object header) 41 00 00 f8 (01000001 00000000 00000000 11111000) (-134217663) 12 4 (object header) 02 00 00 00 (00000010 00000000 00000000 00000000) (2) 16 4 char [C.<elements> N/A 20 4 (loss due to the next object alignment) Instance size: 24 bytes Space losses: 0 bytes internal + 4 bytes external = 4 bytes total ------------------layout8------------------ [C object internals: OFFSET SIZE TYPE DESCRIPTION VALUE 0 4 (object header) 01 00 00 00 (00000001 00000000 00000000 00000000) (1) 4 4 (object header) 00 00 00 00 (00000000 00000000 00000000 00000000) (0) 8 4 (object header) 41 00 00 f8 (01000001 00000000 00000000 11111000) (-134217663) 12 4 (object header) 01 00 00 00 (00000001 00000000 00000000 00000000) (1) 16 2 char [C.<elements> N/A 18 6 (loss due to the next object alignment) Instance size: 24 bytes Space losses: 0 bytes internal + 6 bytes external = 6 bytes total
即一个空对象为8byte的普通对象头+4byte的kcalss+4byte的补充空间
《深入理解Java虚拟机》-周志明