ArrayList - Memory Allocation
I want to know how JVM allocates memory for ArrayList object. for example if i create a object like this
ArrayList a = new ArrayList();
How much memory is allocated initially?
And, if i create a object like
ArrayList a = new ArrayList(5);
How much memory is allocated initially as we dont know about the type of object we are going to put in the arrayList.
And also what happens when the arraylist size exceeds the limit specified initially.
> I want to know how JVM allocates memory for
> ArrayList object. for example if i create a object
> like this
>ArrayList a = new ArrayList();
> is allocated initially?
AFAIK, only enough memory for the variable itself (a pointer to an arrayList variable). I believe that more memory is allocated with each .add() method.
> And, if i create a object like
> ArrayList a = new ArrayList(5);
You don't do this. You don't specify how many items are in the array list. It is completely dynamic, growing and shrinking as needed. It is declared as so:
ArrayList foo = new ArrayList(); // for an ArrayList of Object
ArrayList<MyClass> foo2 = new ArrayList<MyClass>(); // for an ArrayList of MyClass
> ry is allocated initially as we dont know about the
> type of object we are going to put in the arrayList.
> And also what happens when the arraylist size exceeds
> the limit specified initially.
the beauty of ArrayList is that there is no limit specified initially. It is only limited by the heap.
> > I want to know how JVM allocates memory for
> > ArrayList object. for example if i create a
> object
> > like this
> >ArrayList a = new ArrayList();
> > is allocated initially?
>
> AFAIK, only enough memory for the variable itself (a
> pointer to an arrayList variable). I believe that
> more memory is allocated with each .add() method.
>
Uh, have you read the documentation?
Memory is allocated for a set number of elements at creation.
When needed more memory is allocated depending on the specific strategy for each Collection class, which can be a fixed number of elements or a fixed percentage of current capacity.
> > And, if i create a object like
> > ArrayList a = new ArrayList(5);
>
> You don't do this. You don't specify how many items
> are in the array list. It is completely dynamic,
Yes, you can do that. It sets the required initial number of elements (and may determine the growth rate as well).
> > And also what happens when the arraylist size
> exceeds
> > the limit specified initially.
>
> the beauty of ArrayList is that there is no limit
> specified initially. It is only limited by the heap.
No. You CAN provide an initial limit. This can be handy in situations where the size of the List is known in advance as it can make things a bit more efficient (for example if you know in advance that you will need to store exactly 512 elements, declaring the List as having that initial count will prevent reallocation when the default initial size of 10 elements is exceeded (and each time after that when the current size is exceeded).