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

Java中,用于占用大小为n的int []数组的内存等于(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个字节呢?

解决方法

First portion of 4 bytes takes array reference,second – array length,then what takes 8 bytes left?

普通对象开销 – 通常是指示对象类型的几个字节,以及与对象的监视器关联的几个字节.这不是数组特定的 – 你会看到所有的对象.

相关文章

最近看了一下学习资料,感觉进制转换其实还是挺有意思的,尤...
/*HashSet 基本操作 * --set:元素是无序的,存入和取出顺序不...
/*list 基本操作 * * List a=new List(); * 增 * a.add(inde...
/* * 内部类 * */ 1 class OutClass{ 2 //定义外部类的成员变...
集合的操作Iterator、Collection、Set和HashSet关系Iterator...
接口中常量的修饰关键字:public,static,final(常量)函数...