Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java为什么在2D中原语数组和对象数组使用不同的内存_Java_Memory_Multidimensional Array - Fatal编程技术网

java为什么在2D中原语数组和对象数组使用不同的内存

java为什么在2D中原语数组和对象数组使用不同的内存,java,memory,multidimensional-array,Java,Memory,Multidimensional Array,相关代码 int row = 100000; int col = 18; Object[][] objectArray = new Object[row][1]; int[][] intArray = new int[row][1]; System.out.println("Size of objectArray = " + net.sourceforge.sizeof.SizeOf.deepSizeOf(objectArray) + " bytes"); System.out.print

相关代码

int row = 100000;
int col = 18;

Object[][] objectArray = new Object[row][1];
int[][] intArray = new int[row][1];

System.out.println("Size of objectArray  = " + net.sourceforge.sizeof.SizeOf.deepSizeOf(objectArray) + " bytes");
System.out.println("Size of intArray     = " + net.sourceforge.sizeof.SizeOf.deepSizeOf(intArray) + " bytes");

Object[][] objectMatrix = new Object[row][col];
int[][] intMatrix = new int[row][col];

System.out.println("Size of objectMatrix = " + net.sourceforge.sizeof.SizeOf.deepSizeOf(objectMatrix) + " bytes");
System.out.println("Size of intMatrix    = " + net.sourceforge.sizeof.SizeOf.deepSizeOf(intMatrix) + " bytes");
相关输出

Size of objectArray  = 4000024 bytes
Size of intArray     = 4000024 bytes
Size of objectMatrix = 17600024 bytes
Size of intMatrix    = 10400024 bytes
如果不是1D(列数=1),而是2D(列数>1),则对象矩阵占用更多空间

有人能解释一下原因吗

编辑:添加了另一个仅包含一行的案例

    int row = 1;
    int col = 2;

    Object[][] objectArray = new Object[row][1];
    int[][] intArray = new int[row][1];

    System.out.println("Size of objectArray  = " + net.sourceforge.sizeof.SizeOf.deepSizeOf(objectArray) + " bytes");
    System.out.println("Size of intArray     = " + net.sourceforge.sizeof.SizeOf.deepSizeOf(intArray) + " bytes");

    Object[][] objectMatrix = new Object[row][col];
    int[][] intMatrix = new int[row][col];

    System.out.println("Size of objectMatrix = " + net.sourceforge.sizeof.SizeOf.deepSizeOf(objectMatrix) + " bytes");
    System.out.println("Size of intMatrix    = " + net.sourceforge.sizeof.SizeOf.deepSizeOf(intMatrix) + " bytes");
输出

Size of objectArray  = 64 bytes
Size of intArray     = 64 bytes
Size of objectMatrix = 72 bytes
Size of intMatrix    = 64 bytes

对象数组内部引用的大小取决于许多因素(32位对64位),或者如果是64位,是否运行compressedOOPs?因为我通常在64位环境中工作,所以我总是希望对象[]占用更多内存。另一方面,Java中的int定义为32位值,因此使用int[]时,每个值将使用32位加上数组对象本身的一些开销。

我猜对象数组可能占用更多空间。(不知道为什么会这样,但这是可能的,取决于内部实现。)(当然,在完整的64位Java实现中,指针是int的两倍大。)我在第二条评论中提到了这一点。为什么不早点回复?@trutheality——不适用于1的数组,因为它们四舍五入到相同的块大小。对象大小四舍五入到2的某个幂,通常为16或32。因此,一个由14字节元素组成的数组和一个由18字节元素组成的数组很可能会舍入到相同的边界。