结论:16字节,解说如下
jvm默认开启了UseCompressedClassPointers类型指针压缩,查看命令
java -XX:+PrintCommandLineFlags -version
上述图片中的部分解释如下:
! -XX:InitialHeapSize=1073741824 [起始堆大小]
! -XX:MaxHeapSize=17179869184 [最大堆大小]
! -XX:+PrintCommandLineFlags [打印命令行标识]
! -XX:+UseCompressedClassPointers [压缩指针]
! -XX:+UseCompressedOops [OrdinaryObjectPointers 普通对象指针,默认是压缩的(指针4个字节)]
! -XX:+UseParallelGC
! Java HotSpot(TM) 64-Bit Server VM (build 25.261-b12, mixed mode)
JVM64位的,默认指针的长度就是64位(8个字节),由于默认开启了CompressedClassPointers,会将8个字节压缩成4个字节
单位:字节名称 | 对象头 | 对象大小 | ||
名称 | Markword | class Point | Array Length | 无/有数组 |
32位 | 4 | 4 | 4 | 8/16 |
64位 | 8 | 8 | 4 | 16/24 |
64位开启压缩 | 8 | 4 | 4 | 16 |
普通对象
1.markword [64位 8个字节]
2.类型指针class pointer[4个字节]
3.实例数据instance data[int 4字节 long 8个字节]
4.对齐padding [负责补齐不被8整除的部分]
数组对象
1.markword [64位 8个字节]
2.类型指针class pointer[4个字节]
3.实例数据instance data[int 4字节 long 8个字节]
4.数组数据
5.对齐padding [负责补齐不被8整除的部分]
pom:
<dependency>
<groupId>org.openjdk.jol</groupId>
<artifactId>jol-core</artifactId>
<version>0.16</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
</dependency>
code:
package com.java.demo;
import org.openjdk.jol.info.ClassLayout;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ClassLay {
private static final Logger logger = LoggerFactory.getLogger(ClassLay.class);
public static void main(String[] args) {
Object o = new Object();
String s = ClassLayout.parseInstance(o).toPrintable();
logger.debug(s);
}
}
效果图如下: