JS Ext

Thursday, November 22, 2012

Tracking HashMap resizes

In Java, the initial size of a HashMap can be important.  The HashMap size is a power of two and doubles every time the container size exceeds the load ratio (if your HashMap size is 16 with load ratio of 0.75, then after you add the 13th item, the HashMap grows to 32).  The HashMap resize operation is a pretty intensive operation.  It is recommended to minimize the number of resizes a HashMap will do.  The initial size turns out to be pretty important when considering the performance of your application.

One thing that is missing in Java's default HashMap implementation is the ability to track the resizes of a HashMap.  It would be nice to write a log message every time the HashMap resizes.  In this log message, I would like to see the old and new size, as well as the class/line number of the code that created the HashMap.  After reviewing the logs, you can get an idea of which HashMaps are not initially sized correctly. Unfortunatly, the HashMap.resize() method is default scoped, instead of protected scoped, so we can't override the method to add the log message.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.