| Author |
Regd memory allocation
|
J Lalit
Greenhorn
Joined: May 19, 2008
Posts: 15
|
|
Hi,
I want to know how & where jvm gives memory allocation to an arraylist or vector when it is expanded automatically or lets say if arraylist is having default size 16 & say after 12 it will get another 16 blocks then from where & how jvm does this?
|
Thanks & Regards
J Lalit.
|
 |
Sebastian Janisch
Ranch Hand
Joined: Feb 23, 2009
Posts: 1183
|
|
An ArrayList is implemented with an array ... With a starting capacity of 20 or so ..
When the array is full, a new one is created with double the capacity and the old elements are copied into the new array.
And arrays directly point to a space in the memory .. this is why getting elements in an ArrayList is so fast (as opposed to LinkedList) and inserting a lot of elements takes some time, because the array has to be expanded over and over again and the elements need to be copied (as opposed to LinkedList)
|
JDBCSupport - An easy to use, light-weight JDBC framework -
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
Sebastian Janisch wrote:An ArrayList is implemented with an array ... With a starting capacity of 20 or so ..
10 unless otherwise specified in the constructor.
When the array is full, a new one is created with double the capacity and the old elements are copied into the new array.
Not doubled; the new array size will become the old array size * 1.5 + 1, so 50% is added instead of 100%. (The +1 is to make sure the array still gets enlarged if the old array size is 0.)
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32687
|
|
|
In your Java installation folder you will find a file called src.zip. If you unzip that, you can find the ArrayList class and examine the code to see how it works.
|
 |
 |
|
|
subject: Regd memory allocation
|
|
|