I wanted to use a HashSet<Long>
for storing a large list of unique numbers in memory. I calculated the approximate memory to be consumed (in 64 bit pointer size):
Long would take 16 bytes of space. So initially I multiplied the number of entries with 16 to get the memory. But in reality, the memory was much more than 16 bytes per entry. After that I studied HashSet
implementation. In short, in the underlying implementation, it actually stores an extra dummy object (12 bytes) with each entry of hashset. And a pointer (8 bytes) to next entry. Thus conceding extra 12+8 bytes per entry.
So total memory per entry: 16+12+8 = 36 bytes. But still when I ran the code and checked the memory, it was still much more than 36 bytes per entry.
My Question(In short): How much memory does a HashSet
take (for instance, on 64 bit machine)?