相关代码
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(cols数= 1),我有2D(cols数> 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
答案 0 :(得分:1)
对象数组内部引用的大小取决于很多因素(32位对64位),或者如果你是64位,你运行的是compressOOP吗?由于我通常在64位环境中工作,我总是希望Object []占用更多内存。另一方面,Java中的int被定义为32位值,因此使用int [],每个值将使用32位加上数组对象本身的一些开销。