Note that there are some explanatory texts on larger screens.

plurals
  1. POjava why different memory usage for primitive versus object array in 2D
    text
    copied!<p>Relevant code</p> <pre><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"); </code></pre> <p>Relevant output</p> <pre><code>Size of objectArray = 4000024 bytes Size of intArray = 4000024 bytes Size of objectMatrix = 17600024 bytes Size of intMatrix = 10400024 bytes </code></pre> <p>If instead of 1D (number of cols=1), I have 2D (number of cols > 1), the object matrix takes more space.</p> <p>Can someone explain the reason?</p> <p>Edit: Added another case with just one row</p> <pre><code> 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"); </code></pre> <p>Output</p> <pre><code>Size of objectArray = 64 bytes Size of intArray = 64 bytes Size of objectMatrix = 72 bytes Size of intMatrix = 64 bytes </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload