Quick revision
The short answer
The lists keep the large arrays they were created with or grew into, because ArrayList never shrinks on its own. We reduce memory by finishing each list with trimToSize(), or better, by storing an immutable copy with List.copyOf, which keeps exactly the elements and uses compact classes for very small lists. For numeric data we can store primitive arrays instead, and we confirm every change with a heap histogram taken before and after.
What to remember
In this answer, we saw that an ArrayList costs memory by capacity, not size, and never shrinks by itself, so generous pre-sizing or grow-then-shrink leaves millions of mostly empty arrays in a cache. We measured with a class histogram, then stored finished lists as List.copyOf copies, which keep exactly the elements and use compact classes for tiny lists, or trimmed them with trimToSize. For numbers, primitive arrays avoid boxing altogether, and a second histogram confirms the saving.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
The lists keep the large arrays they were created with or grew into, because ArrayList never shrinks on its own. We reduce memory by finishing each list with trimToSize(), or better, by storing an immutable copy with List.copyOf, which keeps exactly the elements and uses compact classes for very small lists. For numeric data we can store primitive arrays instead, and we confirm every change with a heap histogram taken before and after.
Why this question is asked
Memory problems in long-running services often come from many small objects rather than one big one, and a cache of lists is a classic case. Interviewers ask this scenario to see whether we can read a heap dump, explain where the bytes go, and fix it without guessing. In this answer, we will recap how an ArrayList spends memory, see how the waste appears, measure it, and then apply three fixes in order of how much they save, checking the result each time.
1Where an ArrayList Spends Memory
An ArrayList is two objects on the heap: the ArrayList itself, with its size and modCount fields and a reference to the array, and the Object[] array that holds the element references. The array's length is the capacity, and every slot costs a reference, 4 bytes with compressed pointers (the default for heaps under about 32 GB), whether it holds an element or null.
So a list's cost depends on its capacity, not its size. On a typical 64-bit JVM the ArrayList object is about 24 bytes, and an array of capacity 100 is about 416 bytes (a 16-byte header plus 100 times 4). Exact numbers vary with the JVM and its settings, so we always measure.
| How it was created | Array capacity | Approx. bytes |
|---|---|---|
new ArrayList<>(100), 2 adds | 100 | 24 + 416 = 440 |
new ArrayList<>(), 2 adds | 10 | 24 + 56 = 80 |
| grown to 1,000 then cleared to 2 | 1,000 (kept) | 24 + 4,016 = 4,040 |
after trimToSize() | 2 | 24 + 24 = 48 |
List.copyOf(list) | none (2 fields) | about 24 |
2How the Empty Arrays Appear
There are two common ways a cache ends up full of nearly empty arrays. The first is generous pre-sizing: code like new ArrayList<>(100) written for the worst case, while most lists end up with one or two elements. The second is growth followed by shrinking: a list grows while being built, then elements are removed or filtered, and the array stays at its peak size because ArrayList never gives capacity back on its own.
With five million products, the difference between 440 bytes and 24 bytes per list is about 2 GB. That is the shape the heap dump showed: millions of ArrayList objects, each pointing at a big Object[] that is mostly null.
3Measuring Before Changing Anything
Before we fix anything, we take a class histogram, which counts instances and bytes per class. It is much lighter than a full heap dump and can be taken from a running process.
The top line is the evidence: millions of Object[] arrays using far more bytes than the elements could need. In a heap dump tool such as Eclipse MAT we can confirm that these arrays are referenced from ArrayList objects held by our cache, and look at a few of them to see their size versus their length. We save this histogram so we can compare after the fix.
4Fix 1: Trim or Copy When the List Is Finished
The smallest change is to shrink each list when it is complete, just before it goes into the cache. trimToSize() replaces the array with one of exactly size slots. A cleaner option is to store an immutable copy with List.copyOf. That copy holds exactly the elements, and for one or two elements the JDK uses a compact specialised class that keeps them in fields with no array at all; the empty case is a single shared instance.
- Take a class histogrambaseline: instances and bytes
- Find who holds the arraysheap dump, dominator tree
- Are the lists finished when cached?YesStore List.copyOf(list)NotrimToSize() after the last change
- Numeric elements?YesStore int[] or a primitive listNoKeep the object lists
- Take the histogram againcompare bytes per class
5 steps in order. Play them, or step through them one at a time.
The copy has two extra benefits. It is immutable, so a cache entry can be shared safely between threads and nobody can modify it by accident. And List.copyOf rejects null elements, so if the old lists contained nulls we must clean them first or keep using trimmed ArrayLists. The temporary ArrayList used while building becomes garbage at once, which young-generation collection handles cheaply.
5Fix 2: Store Primitives for Numeric Data
If the lists hold numbers, such as List<Long> of ids, the boxing costs more than the arrays. Each Long is a separate object of about 16 bytes plus a 4-byte reference in the array. A long[] stores each value in 8 bytes with no extra objects. So for numeric caches we store long[] directly, or use a primitive collection library such as fastutil or Eclipse Collections, which offer lists of int and long that grow like ArrayList.
Finally, we take the histogram again under the same load. The Object[] line should drop sharply, and the overall live heap after a full collection should fall by roughly the amount we calculated. If it does not, our model of where the memory went was wrong, and we go back to the dump instead of trying more fixes.
6Follow-up questions
Why not just call trimToSize() everywhere?
Because it allocates and copies a new array every time it shrinks, and the next add grows the array again. On lists that are still changing it adds work for no saving. It belongs at the point where a list is finished and will be kept.
Is a heap histogram enough, or do we need a full heap dump?
The histogram tells us which classes use the memory, and that is often enough to confirm a hypothesis. A heap dump tells us who holds those objects, which we need when the cause is not obvious. A dump of a large heap pauses the process and writes a file as big as the live heap, so we take it carefully, ideally from one instance out of rotation.
Could we avoid lists altogether for very small values?
Sometimes. If most entries hold one value, a map of single values with a separate map for the rare multi-value case can be smaller still. But List.copyOf already makes one-element and two-element lists cheap, so we usually stop there unless measurements say otherwise.
7Conclusion
In this answer, we saw that an ArrayList costs memory by capacity, not size, and never shrinks by itself, so generous pre-sizing or grow-then-shrink leaves millions of mostly empty arrays in a cache. We measured with a class histogram, then stored finished lists as List.copyOf copies, which keep exactly the elements and use compact classes for tiny lists, or trimmed them with trimToSize. For numbers, primitive arrays avoid boxing altogether, and a second histogram confirms the saving.
Open with
First I would confirm the picture with a class histogram, using jcmd GC.class_histogram, and keep it as a baseline. The usual cause is that ArrayList memory depends on capacity, not size.
Then, if they want more
Each slot is a 4-byte reference whether it is used or not, and ArrayList never shrinks on its own. So a list created with new ArrayList of 100 for the worst case, or one that grew to a thousand elements and was then filtered down to two, keeps its big array for as long as the cache holds it.
Across millions of entries that is gigabytes of nulls. The fix is to shrink lists at the point where they are finished.
The simplest is trimToSize, but I prefer storing List.copyOf, because it keeps exactly the elements, it is immutable so cache entries are safe to share, and for one or two elements it uses a compact class with no array at all, while the empty list is a single shared instance. I have to watch for nulls, because List.copyOf rejects them.
If the lists hold numbers, I would go further and store long arrays or a primitive list from fastutil or Eclipse Collections, because boxed Longs cost far more than the array slots. Then I take the histogram again under the same load and check that the Object array bytes dropped as much as I calculated.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Collections Framework, overview, java.util, package documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "A heap dump shows millions of ArrayList objects with large empty backing arrays in a...": https://scrivio-interview-library.netlify.app/java/lists/354b14ad13/