【问题标题】:Memory size of a Java 32-bit system int[] arrayJava 32 位系统 int[] 数组的内存大小
【发布时间】:2013-04-12 05:11:32
【问题描述】:

在 Java 中,用于占用大小为 nint[] 数组的内存等于 (4 + n) * 4 字节。

实际上可以通过下面的代码来证明:

public class test {

    public static void main(String[] args) {

        long size = memoryUsed();
        int[] array = new int[2000];
        size = memoryUsed() - size;
        if (size == 0)
            throw new AssertionError("You need to run this with -XX:-UseTLAB for accurate accounting");
        System.out.printf("int[2000] used %,d bytes%n", size);

    }

    public static long memoryUsed() {
        return Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
    }

}

括号中的数字4 非常有趣。 4 字节的第一部分采用数组引用,第二部分 - 数组长度,那么剩下 8 个字节呢?

【问题讨论】:

    标签: java arrays memory int


    【解决方案1】:

    4 字节的第一部分采用数组引用,第二部分 - 数组长度,那么剩下的 8 字节是什么?

    普通对象开销 - 通常是几个字节,指示对象的类型,以及与对象监视器相关的几个字节。这根本不是特定于数组的 - 您会在所有对象中看到它。

    【讨论】:

      猜你喜欢
      • 2012-02-04
      • 1970-01-01
      • 1970-01-01
      • 2011-05-04
      • 2013-11-25
      • 1970-01-01
      • 1970-01-01
      • 2011-05-19
      相关资源
      最近更新 更多