Java中的字节默认情况下占用4个字节? [英] Byte in Java Takes 4 bytes by default?
问题描述
可能重复:
内存中字节的大小 - Java
<我在一篇文章中读到了这篇文章。我只是按原样粘贴:
I read this in an article. I am just pasting as is:
- 该类占用至少8个字节。所以,如果你说
** new Object(); **
,你将在堆上分配8个字节。
- 每个数据成员占用4个字节,除了long和double,占用8个字节。即使数据成员是一个字节,它仍然需要
4字节!此外,使用的内存量在8
字节块中增加。所以,如果你有一个包含一个字节的类,那么
将占用该类的8个字节和8个字节的数据,总计16
字节(呻吟!)。
- The class takes up at least 8 bytes. So, if you say
**new Object();**
you will allocate 8 bytes on the heap.- Each data member takes up 4 bytes, except for long and double which take up 8 bytes. Even if the data member is a byte, it will still take up 4 bytes! In addition, the amount of memory used is increased in 8 byte blocks. So, if you have a class that contains one byte it will take up 8 bytes for the class and 8 bytes for the data, totalling 16 bytes (groan!).
这是真的Java字节分配/需要4个字节吗?而空类需要8个字节?
这里也很混乱。
Is it true Java byte allocates / takes 4 bytes? and empty class takes 8 bytes? Its confusing here as well.
推荐答案
这是假的,至少对于HotSpot JVM来说是错误的。在那里,类中的原始字节字段将占用一个字节,但是类的实例的大小确实从8个字节开始,并且字段被分组为每个占用8个字节的块 - 例如,您可以不要在8字节边界上分割字段。
That is false, at least for the HotSpot JVM. There, a primitive byte field in a class will take one byte, but it is true that the size of an instance of a class starts at 8 bytes, and fields are grouped into chunks that take eight bytes apiece -- for example, you can't split a field across an 8-byte boundary.
参见eg http://www.codeinstructions.com/2008/12/java -objects-memory-structure.html
这篇关于Java中的字节默认情况下占用4个字节?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!