How does memory allocation of an ArrayList work?

后端 未结 5 1556
遇见更好的自我
遇见更好的自我 2021-02-01 02:49

As far as I know, when we are creating an ArrayList:

ArrayList list = new ArrayList(SIZE);

The JVM res

5条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-01 03:52

    If JVM is not able to allocate requested amount of memory it'll throw

    OutOfMemoryError
    

    That's it. Actually JVM memory allocation has only two possible outcomes:

    1. Application is given requested amount of memory.
    2. JVM throws OutOfMemoryError.

    There is no intermediate options, like some amount of memory is allocated.

    It has nothing to do with ArrayList, it's a JVM issue. If you asking whether ArrayList somehow manages this situation in a special way - then answer is "No, it does not." It just tries to allocate amount of memory it needs and lets JVM think about the rest.

提交回复
热议问题