-1

For example, declaring a new int[n][n] in java will result in n array references with each array containing n elements.

If I declare a new int[n][], how much memory will this take? I suspect it is just n references to null, but I want to confirm this.

1 Answers1

2

In Java we have the following sizes:

int = 4 bytes

int[] = 4N + 24 bytes

int[][] ~4MN bytes

Array = 24 bytes + memory for each array entry

So, your array new int[n][] is one-dimensional array from 0 to n. It takes a 4N+24 bytes (24 bytes for array + 4*N bytes for each array entry) typically.

By the way, it is JVM dependent and may be a more accurate answer is ~4N bytes plus a header information.

Sergey Chepurnov
  • 1,317
  • 14
  • 22
  • Do you have any reference for your " + 24 bytes"? If you have, it's possibly outdated. Have a look at http://stackoverflow.com/questions/26357186/what-is-in-java-object-header or related posts. – Erwin Bolwidt Nov 11 '15 at 07:01
  • @Erwin Bolwidt , probably it is JVM dependent. I take this number from algs4.cs.princeton.edu/14analysis. – Sergey Chepurnov Nov 11 '15 at 09:52