3

Relevant 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 output

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

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

Can someone explain the reason?

Edit: Added another case with just one row

    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
17
  • I guess maybe an object array takes more space. (Dunno why this would be, but it's possible, depending on the internal implementation.) Commented Apr 13, 2012 at 18:02
  • 3
    (And, of course, on a full 64-bit Java implementation, pointers are twice as big as ints.) Commented Apr 13, 2012 at 18:03
  • 1
    I mentioned that in the second comment. Why did you not reply sooner? Commented Apr 13, 2012 at 21:22
  • 1
    @trutheality -- Not for an array of 1, since they round to the same block size. Commented Apr 14, 2012 at 5:34
  • 1
    Object size is rounded to some power of 2, generally either 16 or 32. So an array of 1 4-byte element and an array of 1 8-byte element are likely to round to the same boundary. Commented Apr 17, 2012 at 15:43

1 Answer 1

1

The size of the reference inside of the object array depends on many factors (32 bit vs 64 bit) or if you are in 64 bit are you running compressedOOPs? Since I typically work in a 64 bit environment I would always expect the Object[] to occupy more memory. On the other hand an int in Java is defined as a 32bit value, so with an int[] you are going to have 32 bits used for each value plus some overhead for the array object itself.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.