Sometimes it is important to know how much memory you need in order to store the data in a Java collection.
Here is a little overview of the memory overhead of the most important Java collections.
In some cases I also added my own implementations to see how they shape up.
All collections where filled with 10000 Integer elements and then the memory was measured by producing a heap dump and then analyzing the heap dump with MemoryAnalyzer.
Java(TM) SE Runtime Environment (build 1.6.0_23-b05) Java HotSpot(TM) 64-Bit Server VM (build 19.0-b09, mixed mode)
Intel(R) Core(TM) i7 CPU M 620 2.67GHz (4 CPUs), ~2.7GHz
The data stored in the sets is always the same: 10000 instances of
HashSet by wrapping it with the
Java does not provide a ConcurrentHashSet out of the box, but you can create an equivalent by wrapping a
This is an experimental implementation of an array-based mutable
Set that was designed to have minimal memory footprint.
contains() is O(n).
Similar to the
ArraySet above but immutable. Designed for minimal memory footprint.
contains() is O(n).
Another experimental implementation of an array-based immutable
The array is sorted by hash code and
contains() uses a binary search.
contains() is O(log(n)).
ImmutableSortedArraySet has the option to store the hash codes of all elements in a separate
int which trading improved performance with additional memory footprint.
The performance of the different
Sets was measured by running
contains() with an existing random key against a set of a specific size.
Measuring with up to 20 elements shows that
ImmutableArraySet really are linear. With less than 10 elements they are actually faster than the O(log(n))
In the next chart we see the performance with up to 1000 elements. The linear
Sets are no longer shown because they out-scale everything else.
If overall memory utilization is increasing continuously despite garbage collection, there is a memory leak, which will inevitably lead to an out-of-memory error. In this case, a memory heap analysis is necessary.
Sorry for answering so late, there was so much spam that I did not see your comment.
You are of course correct. Memory utilization will increase if you keep references to your object, for example by having added it to a collection.
What I am measuring here is how much memory is needed to maintain the data structure of the collection and how much heap memory can be freed by storing the object data in non-heap memory.
This has nothing to do with memory leaks.