java - How does memory allocation of an ArrayList work? -


as far know, when creating arraylist:

arraylist<string> list = new arraylist<string>(size); 

the jvm reserves a contiguous part of memory. when adding new elements our list, when number of elements reaches 75% of size reserves new, contiguous part of memory , copies of elements.

our list getting bigger , bigger. adding new objects , list has rebuilt once again.

what happens now?

the jvm looking contiguous segment of memory, not find enough space.

the garbage collector can try remove unused references , defragment memory. happens, if jvm not able reserve space new instance of list after process?

does create new one, using maximal possible segment? exception thrown?

i read question java: how arraylist manages memory , 1 of answers is:

reference doesn't consume space. anyhow, of space used. when array getting bigger, problem. cannot forget have got things use memory space.

if jvm not able allocate requested amount of memory it'll throw

outofmemoryerror 

that's it. jvm memory allocation has 2 possible outcomes:

  1. application given requested amount of memory.
  2. jvm throws outofmemoryerror.

there no intermediate options, some amount of memory allocated.

it has nothing arraylist, it's jvm issue. if asking whether arraylist somehow manages situation in special way - answer "no, not." tries allocate amount of memory needs , lets jvm think rest.


Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -