Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HashMap profiling

Are there any HashMap implementations that expose hook methods for profiling the performance of the Map (average chain length, best / worst / average access time, #rehashes, etc.).

It seems quite common to use HashMap and "hope for the best" with regards to ~O(1) access time, without analysing whether this is really the case but I'd like to measure the performance at runtime (at least during development), so anything that hooks into JMX or profiling software would also be good.

Also, Is anyone aware of HashMap implementations where the chains are based on binary trees instead of linked lists?

Thanks in advance.

like image 523
Adamski Avatar asked Jul 02 '09 13:07

Adamski


People also ask

Is HashMap contiguous in memory?

A hashmap is always an array where the hashcode can be determined to get the index of the array element(In jdk this is the entry) . Therefore, it should take a contiguous memory as well.

How are HashMap stored in memory?

A HashMap stores data into multiple singly linked lists of entries (also called buckets or bins). All the lists are registered in an array of Entry (Entry<K,V>[] array) and the default capacity of this inner array is 16.

Are Hashmaps stored in heap?

Just like vectors, hash maps store their data on the heap. This HashMap has keys of type String and values of type i32 .

Why is a String used as a HashMap key in Java?

Since the String class is immutable, you cannot modify the value of a String once it is created. Therefore, it is recommended to use a String variable to hold keys in hash a map.


1 Answers

There's a new Java profiler that goes some way towards doing what you're after. CollectionSpy (www.collectionspy.com) tracks the number of internal rehashes of any hashing container, and also has a graph visualization of the bucket list lenghts. Doesn't (yet) provide any timing info though.

like image 197
Laurence Vanhelsuwe Avatar answered Oct 04 '22 00:10

Laurence Vanhelsuwe