I have a java progam in which there are a lot of HashMap/HashTable being used for mapping key-value pairs. Now I want to profile or rather count how many times the get() and put() methods have been called in my program.
The approach which I was taking is that I extend the Java HashMap/HashTable classes and introduce a member called count and in the get() and the put() methods increment the count everytime the method gets invoked. This would involve a lot of refactoring as I would have to go and remove all the instantiations of the HashMap/HashTables to instead instantiate my extended class. Is this approach reasonable or is there other better way of maintaining this count?
The best solution for such a task is to use a Profiler such as YourKit or JProfiler. A profiler performs “instrumentation” on all (or a subset of) the classes loaded by the profiled JVM to perform exactly what you need: Count and measure all of the method invocations, without a single line of modified code.
Both of the aforementioned profilers ship with trial licenses. Once you have tried them, you’ll probably buy one because they are very useful in so many situations.