相关代码
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