Java why different memory usage for primitive and array of objects in 2D

Corresponding code

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");

Relevant Conclusion

Size of objectArray  = 4000024 bytes
Size of intArray     = 4000024 bytes
Size of objectMatrix = 17600024 bytes
Size of intMatrix    = 10400024 bytes

If instead of 1D (cols = 1), I have 2D (cols> 1), the object matrix takes up more space.

Can someone explain the reason?

Edit: added one more line case

    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");

Output

Size of objectArray  = 64 bytes
Size of intArray     = 64 bytes
Size of objectMatrix = 72 bytes
Size of intMatrix    = 64 bytes
+3
source share
1 answer

(32- 64-), 64- , OOP? 64- , , Object [] . , int Java 32- , int [] 32 , .

+1

All Articles