public class Student {
privateString name;
privateinthigh;
privateInteger age;
privatelongjj;
privateLong weight;
privateBigDecimal income;
}
System.out.println(VMSupport.vmDetails());
System.out.println(ClassLayout.parseClass(Student.class).toPrintable());
---------------华丽分割线------------------
Running32-bit HotSpot VM.
Objectsare 8 bytes aligned.
Fieldsizes by type: 4, 1, 1, 2, 2, 4, 4, 8, 8 [bytes]
Arrayelement sizes: 4, 1, 1, 2, 2, 4, 4, 8, 8 [bytes]
com.whl.Studentobject internals:
OFFSET SIZE TYPE DESCRIPTION VALUE
0 8 (object header) N/A --class区域占用4byte,mark区域占用最少4byte
8 8 long Student.jj N/A --基本类型long占用8字节
16 4 int Student.high N/A
20 4 String Student.name N/A
24 4 Integer Student.age N/A
28 4 Long Student.weight N/A --包装对象所以该位置是reference占用4字节
32 4 BigDecimal Student.income N/A
36 4 (loss due to the next object alignment)
Instancesize: 40 bytes (estimated, the sample instance is not available)
Spacelosses: 0 bytes internal + 4 bytes external = 4 bytes total
//offset=offset+size
无论是32位还是64位的HotSpot,使用的都是8字节对齐。也就是说每个java对象,占用的字节数都是8的整数倍
(对象头+实例数据+padding)%8等于0且0<= padding < 8
1.基本数据类型占用的字节数,JVM规范中有明确的规定,无论是在32位还是64位的虚拟机,占用的内存大小是相同的。
2.reference类型在32位JVM下占用4个字节,但是在64位下可能占用4个字节或8个字节,这取决于是否启用了64位JVM的指针压缩参数UseCompressedOops。
3.newObject()这个对象在32位JVM上占8个字节,在64位JVM上占16个字节。
4.开启(-XX:+UseCompressedOops)指针压缩,对象头占12字节;关闭(-XX:-UseCompressedOops)指针压缩,对象头占16字节。
5.64位JVM上,数组对象的对象头占用24个字节,启用压缩之后占用16个字节。之所以比普通对象占用内存多是因为需要额外的空间存储数组的长度。
6.对象内存布局中的实例数据,不包括类的static字段的大小,因为static字段是属于类的,被该类的所有对象共享。
另外Byte,Short,Integer,Long,Character这5种整型的包装类也只是在对应值小于等于127时才可使用对象池,也即对象不负责创建和管理大于127的这些类的对象