一个Java 32位系统的内存大小INT []数组 [英] Memory size of a Java 32-bit system int[] array
问题描述
在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 sizen
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 of4
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屋!