java中对象的大小是多少 [英] What is the size of the object in java

查看:63
本文介绍了java中对象的大小是多少的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

众所周知,java使用以下数据类型

As we all know that java uses the following data types

byte    Occupy 8 bits in memory
short   Occupy 16 bits in memory
int     Occupy 32 bits in memory
long    Occupy 64 bits in memory 

如果我创建类似的类

class Demo{
    byte b;        
    int i;
    long l;
}

Demo obj = new Demo();

现在我的问题是obj的大小是< or > or =的大小b+i+l的大小是104 bytes.请以适当的理由给我澄清.

Now my question is obj size is < or > or = the size of b+i+l which is 104 bytes. Please give me the clarification with proper reason.

谢谢,
阿尼尔·库马尔(Anil Kumar C)

Thanks,
Anil Kumar C

推荐答案

对象的内存大小取决于体系结构,主要取决于VM是32位还是64位.实际的VM实施也很重要.

The in-memory size of the object depends on the architecture, mainly on whether the VM is 32 or 64-bit. The actual VM implementation also matters.

对于每个对象,您需要为其对象标头(在64位VM上通常为2 * 8字节)及其字段(根据VM的实现,需要额外的对齐空间)留出空间.然后将最后的空间四舍五入到单词大小的最接近倍数.

For each object, you need space for its object header (typically 2*8 bytes on 64-bit VMs), its fields (extra space for alignment depending on the VM implementation). The final space is then rounded up to the nearest multiple of the word size.

这篇关于java中对象的大小是多少的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆