File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Regd memory allocation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Customer Requirements for Developers this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Regd memory allocation" Watch "Regd memory allocation" New topic
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: 20049
    
  30

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 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 43399
    
  32
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.
 
Don't get me started about those stupid light bulbs.
 
subject: Regd memory allocation
 
It's not a secret anymore!