• Post Reply Bookmark Topic Watch Topic
  • New Topic

How JVM allocates memory on heap for ArrayList?  RSS feed

 
Deepak Kumar
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

Arraylist grows dynamically.

when a new ArrayList() is made it constructs an empty list with an initial capacity of ten, however,
when we keep on adding the elements to the ArrayList, it grows dynamically on the heap, how the JVM manages to allocate the memory on heap ? Is there any load factor used by JVM? and Also what is the use of the method ensureCapacity()?

Thank You,
Deepak Kumar G.S
 
Ireneusz Kordal
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at source code here:
http://www.docjar.com/html/api/java/util/ArrayList.java.html

This is a code of method 'add':


and here of 'ensureCapacity'


ArrayList grows when there is no room for adding a new element,
the 'growth factor' is: (oldCapacity * 3) / 2 + 1 ---> about 75% of previous size.
 
Deepak Kumar
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Ireneusz Kordal
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!