Just a general question about array allocation, primarily in Java but I imagine it is relevant to all programming languages:
How long does it take to allocate memory for an array of size n [in terms of O(n)]? I could imagine an implementation where the memory allocation happens in constant time: if you have a large quantity of empty memory you could just create a pointer to the first and last index of the new array, but is that how the memory is generally allocated? (Also, at least in Java, if you initialize an array of integers, all of the values in the array with be initially set to 0; does that mean that each of the indices in the array are individually set to equal 0, which would make the operation O(n)?)
Thanks.
The ArrayList in Java is backed by an array. So let's focus first on the time complexity of the common operations at a high level: add() – takes O(1) time; however, worst-case scenario, when a new array has to be created and all the elements copied to it, it's O(n) add(index, element) – on average runs in O(n) time.
Where is the memory allocated for an array in Java? Memory is allocated in Heap are for the Array in Java. In Java reference types are stored in the Heap area. As arrays are also reference types, (they can be created using the “new” keyword) they are also stored in the Heap area.
The memory allocation for an array includes the header object of 12 bytes plus the number of elements multiplied by the size of the data type that will be stored and padding as needed for the memory block to be a multiple of 8 bytes.
I just ran a micro benchmark on hotspot - post JIT compilation, allocating an array (on an i7) takes:
So to answer your question, it seems empirically that it is O(n) on hotspot.
Detailed results:
Benchmark Mode Thr Cnt Sec Mean Mean error Units
c.a.p.ArrayVsList.createArray1 avgt 1 5 2 12.293 0.867 nsec/op
c.a.p.ArrayVsList.createArray10000 avgt 1 5 2 428.369 9.997 nsec/op
c.a.p.ArrayVsList.createArray1M avgt 1 5 2 342972.975 7253.989 nsec/op
null
(or 0 for a primitive) at the time the array is createdYou have already answered the question by yourself, it is O(n)
in Java since elements are initialized, while there are languages where this operation is O(1)
since there is no initialization.
and just to be sure
public class ArrayTest {
public static void main(String[] args) {
int[] var = new int[5];
}
}
produces
public class ArrayTest extends java.lang.Object{
public ArrayTest();
Code:
0: aload_0
1: invokespecial #1; //Method java/lang/Object."<init>":()V
4: return
public static void main(java.lang.String[]);
Code:
0: iconst_5
1: newarray int
3: astore_1
4: return
}
and documentation says about newarray:
A new array whose components are of type atype and of length count is allocated from the garbage-collected heap. A reference arrayref to this new array object is pushed into the operand stack. Each of the elements of the new array is initialized to the default initial value for the type of the array (§2.5.1).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With