java对象内存结构
2015-08-19 16:12
567 查看
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的这些类的对象
{
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的这些类的对象
相关文章推荐
- 打印2的n次方数字三角形
- java 回调的使用
- 杭电 HDU 1402 A * B Problem Plus JavaJ大数
- java concurrent包自带线程池和队列详细讲解
- struts2 和 ajax的交互
- Win10系统 JDK环境变量配置方法
- Java内存泄露例子
- java并发编程-Executor框架
- 我的eclipse插件推荐
- MyBatis <set>标签的使用
- 如何在 Java 中正确使用 wait, notify 和 notifyAll – 以生产者消费者模型为例
- 【微信】微信获取TOKEN,以及储存TOKEN方法,Spring quartz让Token永不过期
- 如何在 Java 中正确使用 wait, notify 和 notifyAll – 以生产者消费者模型为例
- mybatis Java API
- J-4 Java语言基础
- java.lang.UnsatisfiedLinkError: Create
- java向文件中追加内容与读写文件内容源码
- Windows环境下教你用Eclipse ADT 插件生成.h/.so文件,Java下调用JNI,轻松学习JNI
- java实现二叉树的遍历
- spring+quartz 动态任务方案