一个Java 32位系统的内存大小INT []数组 [英] Memory size of a Java 32-bit system int[] array

查看:256
本文介绍了一个Java 32位系统的内存大小INT []数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中,用于占据大小的 INT [] 阵列存储 N 等于(4 + N)* 4 字节。

实际上可以通过下面的code被证明

 公共类的测试{    公共静态无效的主要(字串[] args){        长尺寸= memoryUsed();
        INT []数组=新INT [2000];
        大小= memoryUsed() - 大小;
        如果(大小== 0)
            抛出新AssertionError异常(您需要使用-XX运行此:-UseTLAB为准确核算);
        System.out.printf(INT [2000]用于%d字节%N,大小);    }    公共静态长memoryUsed(){
        返回调用Runtime.getRuntime()totalMemory() - 调用Runtime.getRuntime()freeMemory();。
    }}

如此有趣的是数 4 在括号中。的 4 字节第一部分需要数组引用,第二个 - ?数组的长度,那还有什么需要留下8个字​​节


解决方案

  

4字节的第一部分需要数组引用,第二个 - ?数组的长度,那还有什么需要留下8个字​​节


正常对象的开销 - 典型地为几个字节指示对象的类型,并与显示器的对象相关联的几个字节。这不是针对阵列在所有 - 你会看到它的所有对象

In Java, memory used for occupying the int[] array of size n equals to (4 + n) * 4 bytes.

Practically can be proven by the code below:

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();
    }

}

so interesting is number 4 in parentheses. First portion of 4 bytes takes array reference, second - array length, then what takes 8 bytes left?

解决方案

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

Normal object overhead - typically a few bytes indicating the type of the object, and a few bytes associated with the monitor for the object. This is not array-specific at all - you'll see it for all objects.

这篇关于一个Java 32位系统的内存大小INT []数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆