加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

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

发布时间:2020-12-15 00:26:15 所属栏目:Java 来源:网络整理
导读:在 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 (s
在 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?

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读