• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regd memory allocation

 
J Lalit
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
Rob Spoor
Sheriff
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic