What is the memory size of an ArrayList in Java
This is what a memory profiler is for. It will tell you for your platform. The minimum size for an empty ArrayList is 64-bytes. It is highly likely you don't need to know this unless you have 100K elements or more.
You can use something like Runtime.getRuntime().totalMemory()
and its counterpart Runtime.getRuntime().freeMemory()
to get an educated guess, but that doesn't account for objects that are GC'ed between calls.
It's the capacity of the java.util.ArrayList multiplied by the reference size (4 bytes on 32bit, 8bytes on 64bit) + [Object header + one int and one references]
.
The capacity is different (always >=)
than the size but you want to make 'em equal, call trimToSize()
Technically speaking the ArrayList
has an Object[]
where it stores the data.