Course contentsShow
← Java
Lesson 1010 of 2,87024. List ImplementationsFree lesson

Memory Implications of Capacity Management

Understand the trade-offs between wasted space and reallocation frequency in ArrayList design.

Memory Implications of Capacity Management

What you'll learn: How ArrayList's capacity growth strategy creates trade-offs between memory waste and performance overhead.

The Core Trade-Off

Every time an ArrayList grows, it must choose between two competing concerns:

  1. Minimize wasted space – Keep capacity close to actual size
  2. Minimize reallocations – Grow aggressively to avoid frequent copies

The 1.5× growth strategy you learned earlier strikes a balance, but it's not free.

Understanding Memory Waste

When an ArrayList expands from capacity 10 to 15, then you add just one element (size = 11), you have 4 unused slots. That's about 27% wasted space. If you know you'll only ever need 11 elements, that's memory sitting idle—especially problematic when you have thousands of ArrayList instances.

This is why trimToSize() exists: it reallocates the backing array to exactly match the size, eliminating waste when you're done adding elements.

Understanding Reallocation Cost

Conversely, if ArrayList grew by just 1 slot each time (minimize waste), every add() beyond initial capacity would trigger System.arraycopy(). Adding 1000 elements would mean 990+ array copies—devastating for performance.

The 1.5× strategy means you copy less frequently, but pay for it with unused capacity between growth spurts.

Real-World Guidance

  • If you know the final size at construction time, specify initial capacity to avoid all reallocations and waste.
  • For long-lived lists that won't grow further, call trimToSize() after bulk inserts to reclaim memory.
  • For frequently modified lists, accept some wasted space as the cost of speed.

Key Takeaway: ArrayList capacity management is a classic space-time tradeoff—growing conservatively wastes CPU on copies, growing aggressively wastes memory on unused slots, and the 1.5× factor is a practical middle ground.