繁体   English   中英

Java 32位系统int []数组的内存大小

[英]Memory size of a Java 32-bit system int[] array

在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个字节?

4个字节的第一部分采用数组引用,第二个数组采用长度,然后剩下8个字节?

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

暂无
暂无

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

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