How much memory takes TreeMap<Long, Long>
collection filled with 1000 (all unique) key-value pairs?
Yes I can just watch memory dump, but need to know granularly why exactly:
Long
= n bytesEntry<Long, Long >
= 2n bytes, so 1000 entries = 2000n bytes- reference in tree node: k bytes, 1 node have 2 links to children, so 2k bytes,
- etc.
Total: X ?
CodePudding user response:
JOL (Java Object Layout)
To answer such questions you can use the tool, JOL.
In your case it will get such results:
java.util.TreeMap@17046283d footprint:
COUNT AVG SUM DESCRIPTION
2000 24 48000 java.lang.Long
1 48 48 java.util.TreeMap
1000 40 40000 java.util.TreeMap$Entry
3001 88048 (total)
Total 88048 bytes used
Granularly
TreeMap itself:
OFF SZ TYPE DESCRIPTION VALUE
0 8 (object header: mark) N/A
8 4 (object header: class) N/A
12 4 Set<K> AbstractMap.keySet N/A
16 4 Collection<V> AbstractMap.values N/A
20 4 int TreeMap.size N/A
24 4 int TreeMap.modCount N/A
28 4 Comparator<? super K> TreeMap.comparator N/A
32 4 Entry<K, V> TreeMap.root N/A
36 4 EntrySet TreeMap.entrySet N/A
40 4 KeySet<K> TreeMap.navigableKeySet N/A
44 4 NavigableMap<K, V> TreeMap.descendingMap N/A
Instance size: 48 bytes
TreeMap.Entry
OFF SZ TYPE DESCRIPTION VALUE
0 8 (object header: mark) N/A
8 4 (object header: class) N/A
12 1 boolean Entry.color N/A
13 3 (alignment/padding gap)
16 4 K Entry.key N/A
20 4 V Entry.value N/A
24 4 Entry<K, V> Entry.left N/A
28 4 Entry<K, V> Entry.right N/A
32 4 Entry<K, V> Entry.parent N/A
36 4 (object alignment gap)
Instance size: 40 bytes
And Long:
OFF SZ TYPE DESCRIPTION VALUE
0 8 (object header: mark) N/A
8 4 (object header: class) N/A
12 4 (alignment/padding gap)
16 8 long Long.value N/A
Instance size: 24 bytes
Also you may need do read additional questions about alignment and object header.