An int[]
will consume less memory than an ArrayList<Integer>
. Part of that is simply the overhead which is added from an Integer
which adds ~16 bytes per instance. This video goes through memory impact of various objects and collections in 32bit and 64bit jvms. At about the 9:30 mark it talks about memory associated with each object. At about the 11:15 mark it talks about how much memory various types (including Object
references) take.
For an int[]
, you have 1 Object
(the int[]
) and it will actually contain all of the individual int
values as contiguous memory.
For an ArrayList<Integer>
, you have the ArrayList
object, the Object[]
object and all of the Integer
objects. Additionally, the Object[]
doesn't actually contain the Integer
objects in contiguous memory, rather it contains object references in contiguous memory. The Integer
objects themselves are elsewhere on the heap.
So the end result is that an ArrayList<Integer>
requires ~6x the amount of memory as an int[]
. The backing Object[]
and the int[]
take the same amount of memory (~40,000 bytes). The 10k Integer
objects take ~20 bytes each for a total of 200,000 bytes. So the ArrayList
will be a minimum of 240,000 bytes compared to the int[]
at approximately 40,000 bytes.