In Java 8, why is the default capacity of ArrayList now zero?

前端 未结 6 1406
再見小時候
再見小時候 2020-11-27 13:34

As I recall, before Java 8, the default capacity of ArrayList was 10.

Surprisingly, the comment on the default (void) constructor still says: Cons

6条回答
  •  南方客
    南方客 (楼主)
    2020-11-27 13:40

    The question is 'why?'.

    Memory profiling inspections (for example (https://www.yourkit.com/docs/java/help/inspections_mem.jsp#sparse_arrays) shows that empty (filled with nulls) arrays occupy tons of memory .

    Default size of 10 objects means that we allocate 10 pointers (40 or 80 bytes) for underlying array at creation and fill them in with nulls. Real java applications create millions of array lists.

    The introduced modification removes^W postpone this memory consumption till moment you will actually use the array list.

提交回复
热议问题