简体   繁体   English

当间接在数组内时,字节原语是否成为内存中的字节?

[英]Does a byte primitive become a byte in memory when indirectly inside an array?

I have heard that a byte by itself takes up 4 bytes in memory, and a byte inside a byte array takes 1 byte, but what about a byte member variable inside an object that is within an array? 我听说一个字节本身在内存中占用4个字节,而一个字节数组中的一个字节占用1个字节,但是在一个数组内的对象内的字节成员变量呢?

class SomeObject {
    byte iBite;
}

public static void main(String[] args) {
    SomeObject[] objs = ...
}

Will each SomeObject have its iBite variable only be 1 byte in memory? 每个SomeObject的iBite变量在内存中只有1个字节吗?

A byte as a local variable is implemented as an int , so it takes 4 bytes. 作为局部变量的字节实现为int ,因此需要4个字节。

A byte as a field of a class (like in your example) takes 1 byte of memory, but classes in memory are rounded up to multiples of 8 bytes on eg HotSpot JVMs. 作为字段的字节(如示例中所示)占用1个字节的内存,但内存中的类在例如HotSpot JVM上向上舍入为8个字节的倍数。 That said, if you have a class with multiple byte fields (or char or short fields), those will make more efficient use of memory. 也就是说,如果你有一个具有多个byte字段(或charshort字段)的类,那么这些char将更有效地使用内存。

Arrays are similar: each byte will take 1 byte, but the array as a whole will be rounded up to a multiple of 8 bytes on eg HotSpot JVMs. 数组相似:每个byte占用1个字节,但整个数组将在例如HotSpot JVM上向上舍入为8个字节的倍数。

You can experiment with this by hand using http://openjdk.java.net/projects/code-tools/jol/ . 您可以使用http://openjdk.java.net/projects/code-tools/jol/手动试验。 If you use it, for example, on 如果您使用它,例如,打开

public static class A {
    boolean f;
    byte g;
    int h;
}

I get 我明白了

Running 64-bit HotSpot VM.
Using compressed oop with 3-bit shift.
Using compressed klass with 3-bit shift.
Objects are 8 bytes aligned.
Field sizes by type: 4, 1, 1, 2, 2, 4, 4, 8, 8 [bytes]
Array element sizes: 4, 1, 1, 2, 2, 4, 4, 8, 8 [bytes]

org.openjdk.jol.samples.JOLSample_01_Basic.A object internals:
 OFFSET  SIZE    TYPE DESCRIPTION                    VALUE
      0    12         (object header)                N/A
     12     4     int A.h                            N/A
     16     1 boolean A.f                            N/A
     17     1    byte A.g                            N/A
     18     6         (loss due to the next object alignment)
Instance size: 24 bytes (estimated, the sample instance is not available)
Space losses: 0 bytes internal + 6 bytes external = 6 bytes total

which exhibits pretty clearly that boolean and byte take one byte as object fields. 很清楚地表booleanbyte占用一个字节作为对象字段。

As you'd expect, char and short are 2 bytes, int and float are 4 bytes, long and double are 8 bytes. 正如您所期望的, charshort是2个字节, intfloat是4个字节, longdouble是8个字节。

https://stackoverflow.com/a/14782255/869736 explains some details on Dalvik, including that currently small fields like byte are in fact implemented with 4 bytes. https://stackoverflow.com/a/14782255/869736解释了有关Dalvik的一些细节,包括目前像byte这样的小字段实际上是用4个字节实现的。 Remember that these details are going to be VM-dependent. 请记住,这些细节将成为虚拟机相关的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM