Quick revision
The short answer
Each element of a List<Double> is a separate Double object on the heap, with an object header of 12 to 16 bytes plus 8 bytes of data, padded to 24 bytes, and a reference to it in the list's backing array. So ten million readings take around 280 MB instead of the 80 MB a double[] needs. I would confirm the cost with a heap histogram, then switch the hot data to a primitive array, a primitive collection or a columnar layout.
What to remember
In this answer, we saw that a List<Double> stores every reading as a separate Double object, about 24 bytes with its header and padding, plus a 4-byte reference, so ten million readings take around 280 MB instead of 80 MB in a double[]. We confirmed the numbers with a class histogram, saw that boxing also adds GC work and cache misses, and compared the fixes: primitive arrays, primitive collection libraries and columnar layouts, with Valhalla as future work rather than a plan.
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.
Each element of a List<Double> is a separate Double object on the heap, with an object header of 12 to 16 bytes plus 8 bytes of data, padded to 24 bytes, and a reference to it in the list's backing array. So ten million readings take around 280 MB instead of the 80 MB a double[] needs. I would confirm the cost with a heap histogram, then switch the hot data to a primitive array, a primitive collection or a columnar layout.
Why this question is asked
Memory surprises like this one are common in services that keep data in memory for fast access, such as caches, time series buffers or batch jobs. The code looks harmless, because List<Double> is the most natural type to write, but the boxed representation can make the heap three or four times larger than the data and keep the garbage collector busy. Interviewers ask this scenario to see whether we can explain the object layout behind the number and choose a fix that fits the situation. In this answer, we will recap what boxing is, work out the memory of one Double, measure it on a real JVM, look at the effect on garbage collection and caches, and then compare the options for fixing it.
1A Quick Recap of Boxing
Java generics only work with objects, so a List cannot hold the primitive double directly. When we add a double to a List<Double>, Java wraps it in a Double object, which is called boxing. When we read it back as a double, it unwraps it again, which is called unboxing. Both happen automatically, so the code never shows the cost.
A double[] works differently. It is one object that holds all the values side by side in a single block of memory, 8 bytes each. There is nothing to wrap, and reading element 42 is a single memory access at a fixed offset.
2Where the Memory Goes
Every object on the HotSpot JVM starts with a header. It holds a mark word, used for locking, hashing and garbage collection, and a pointer to the object's class. With compressed class pointers, the default on a 64-bit JVM with a normal heap size, the header is 12 bytes. Without them it is 16 bytes. Java 25 also offers compact object headers (enabled with -XX:+UseCompactObjectHeaders), which shrink the header to 8 bytes, so check your JVM flags before trusting exact numbers.
A Double adds its 8-byte value to the 12-byte header, which gives 20 bytes, and objects are aligned to 8 bytes, so it is padded to 24 bytes. The ArrayList also stores a reference to each Double in its backing Object[] array, which is 4 bytes with compressed references. And because an ArrayList grows by half its size when it is full, the backing array may have spare slots too.
| Part | List<Double> | double[] |
|---|---|---|
| Value | 8 bytes inside each Double | 8 bytes in the array |
| Object header | 12 bytes per Double | one header for the whole array |
| Padding | 4 bytes per Double | none per element |
| Reference | 4 bytes in the Object[] | none |
| Total per reading | about 28 bytes | 8 bytes |
| Ten million readings | about 280 MB, plus growth slack | about 80 MB |
| Objects for GC to trace | 10 million and one array | one array |
3Measuring It Before Changing Anything
Before we change the code, we confirm the cause. A class histogram lists every class on the heap with its instance count and total bytes, and we can take one from a running JVM with jcmd.
The numbers tell the whole story: ten million Double instances at exactly 24 bytes each, 240 MB, plus a 40 MB reference array. (The process id and the exact sizes will differ on your machine.) If we want to see the layout of a single object, the JOL library (Java Object Layout) prints the header, the field offsets and the padding of a class on the running JVM, which is the most direct way to confirm the per-object overhead.
4Why It Is Slow as Well as Big
The extra memory is not the only cost. Each Double is an allocated object, so building the list creates ten million objects that the garbage collector has to trace and, in a generational collector, copy at least once before they reach the old generation. That shows up as longer or more frequent pauses and more CPU spent in GC threads.
Reading the data is slower too. To sum the list, the processor reads a reference from the backing array and then jumps to wherever that Double happens to live. The objects may be spread across the heap, so many of those jumps miss the CPU cache. A double[] is read in order from one block of memory, which the processor can prefetch, so a loop over it often runs several times faster.
5The Options for Fixing It
The simplest fix is a plain double[] when we know the size in advance or can grow it ourselves. It gives the smallest memory and the fastest loops, at the cost of losing the List API.
If we need a growable list, a primitive collection library such as fastutil or Eclipse Collections provides a DoubleArrayList that stores a double[] inside but offers list-like methods. That adds a dependency, which is a fair price on a proven hot path.
If each reading has several fields, such as a timestamp, a sensor id and a value, a columnar layout often works best: one long[] for the timestamps, one int[] for the sensor ids and one double[] for the values, with the same index tying them together. Ten million readings then cost 8 + 4 + 8 = 20 bytes each, instead of a Reading object plus a boxed Double for each one.
We may hear that Project Valhalla will fix this with value classes that can be stored flat. That work is promising, but it is not a final feature in Java 25, so we should not design today's service around it.
6Follow-up questions
How would you confirm the per-object overhead on your JVM?
I would take a class histogram with jcmd <pid> GC.class_histogram and divide the bytes by the instance count for java.lang.Double, which gives the real size on that JVM with its current flags. For more detail, the JOL library prints the exact layout of a class, including header size, field offsets and padding. Both matter because the answer changes with compressed references, heap size and, in Java 25, compact object headers.
When is the boxed list still the right choice?
When the list is small, when it is not on a hot path, or when an API we must call expects a List<Double>. It is also the natural choice when a missing reading must be represented as null, although a double[] with NaN or a separate presence bitmap can do the same job. For a few thousand values, the clarity of a normal list is worth more than a few hundred kilobytes.
Would a LinkedList<Double> be better or worse?
Much worse. Each element then needs a node object with a header, three references and padding, about 24 bytes on its own, in addition to the 24-byte Double. LinkedList is rarely the right choice for large data.
7Conclusion
In this answer, we saw that a List<Double> stores every reading as a separate Double object, about 24 bytes with its header and padding, plus a 4-byte reference, so ten million readings take around 280 MB instead of 80 MB in a double[]. We confirmed the numbers with a class histogram, saw that boxing also adds GC work and cache misses, and compared the fixes: primitive arrays, primitive collection libraries and columnar layouts, with Valhalla as future work rather than a plan.
Open with
The problem is boxing. A List<Double> cannot hold primitives, so each of the ten million readings is a separate Double object. On a typical 64-bit HotSpot JVM that object has a 12-byte header and 8 bytes of value, padded to 24 bytes, and the ArrayList's backing array holds a 4-byte reference to it, so it is about 28 bytes per reading, roughly 280 MB, against 80 MB for a double array.
Then, if they want more
It also means ten million objects for the garbage collector to trace and copy, and poor cache locality, because summing the list chases a pointer to a different place for every element. Before changing anything I would confirm it with jcmd GC.class_histogram, which should show ten million java.lang.Double instances, and JOL if I want the exact layout.
Then I would pick the fix by need. If the size is known, a plain double array. If I need a growable list, a primitive collection such as fastutil's DoubleArrayList. If each reading has several fields, a columnar layout with parallel primitive arrays. I would keep the boxed list where the data is small or nulls are meaningful.
I would not count on Valhalla value classes, because they are not a final feature in Java 25. After the change I would measure heap usage and GC time again to confirm the gain.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 25
- 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 Java Language Specification, Java SE 21, Java SE 21 API 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 service stores ten million sensor readings in memory as List<Double>. Heap usage is...": https://scrivio-interview-library.netlify.app/java/types-and-values/40c510bb1e/