Reputation: 69
Here's the question about memory allocation in Java.
Suppose I have an array of ints A[100] and another array of ints B[10][10]. Do they need the same amount of memory in Java or is it different? If the latter, what's the difference and how does it grow with N?
I'm talking here only about Ns that are power of 2 of a positive number, so we're talking here about square 2D arrays and their possible 1D representation.
Upvotes: 4
Views: 2862
Reputation: 20185
Definitively, no.
In C/C++ a 2D-array allocates all memory for the 2D-array in "one chunk".
In Java, a 2D-array is an "array of arrays". One-dimensional arrays are allocated in one chunk, but the one-dimensional arrays may be scattered. Furthermore, the "outer" array (2nd dimension) needs heap memory as well, storing the references to the 1D-arrays.
So if one allocates a 2D-array of dimensions m
(outer) and n
(inner), Java will create one array of m
elements and m
arrays ofn
elements each. The outer array just stores the references to the m
inner arrays.
This page gives a nice explanation and visualization of multidimensional arrays in Java.
Upvotes: 9
Reputation: 815
When an object is creating by using “new”, memory is allocated on the heap and a reference is returned. This is also true for arrays, since arrays are objects.
int arr[] = new int3; The int[] arr is just the reference to the array of 3 integer. If you create an array with 10 integer, it is the same – an array is allocated and a reference is returned.
Actually, we can only have one dimensional arrays in Java. 2D arrays are basically just one dimensional arrays of one dimensional arrays.
int[ ][ ] arr = new int[3][ ];
arr[0] = new int[3];
arr[1] = new int[5];
arr[2] = new int[4];
Multi-dimensional arrays use the name rules.
As you can observe that while managing a 2D array extra 1D array is needed thus in terms of memory the size differ but from users view both have same size.
Images were taken form this site.
Upvotes: -1
Reputation: 26185
This is empirical confirmation of @Turing85's answer, and measurement of the overhead. This program alternately allocates and frees a single array of a million elements or an int[1000][1000], reporting the amount of memory in use at each step. It quickly settles down to:
Neither: 291696
1D: 4291696
Neither: 291696
2D: 4311696
showing an extra 20,000 bytes of memory in use for the 2D case.
Here is the program:
public class Test {
public static void main(String[] args) {
int M=1000;
for(int i=0; i<10; i++){
System.out.println("Neither: "+inUseMem());
int[] oneArray = new int[M*M];
System.out.println("1D: "+inUseMem());
oneArray = null;
System.out.println("Neither: "+inUseMem());
int[][] twoArray = new int[M][M];
System.out.println("2D: "+inUseMem());
twoArray = null;
}
}
private static long inUseMem() {
System.gc();
return Runtime.getRuntime().totalMemory()
- Runtime.getRuntime().freeMemory();
}
}
Running this program on the system of interest using the actual array sizes should show the cost of using the 2D array. If the arrays really are around 10,000 elements total, it is probably best to go with whatever makes the code more readable.
Upvotes: 2