Take the 2-minute tour ×

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?

share|improve this question
I think you could do this with reflection and inserting hooks on methods. Initial googling suggests it's possible, at least. –  bdares Aug 29 '12 at 7:08
God bless search & replace all! :P –  Averroes Aug 29 '12 at 7:09
@bdares Could you elaborate on the reflection aspect (I understand Reflection) on how it would be done. –  user1420750 Aug 29 '12 at 7:18
add comment

4 Answers

up vote 1 down vote accepted

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.

share|improve this answer
+1 This is best approach. If this is not an option you can modify HashMap or Hashtable for testing purposes. –  Peter Lawrey Aug 29 '12 at 7:12
@PeterLawrey I agree this is the best approach but not an option right now. I think I will go ahead and modify the HashMap|HashTable and continue the way I was going about it. –  user1420750 Aug 29 '12 at 7:21
To modify the original you need to pre-pend them to your bootclasspath or add them to a jar in the lib/endorsed directory –  Peter Lawrey Aug 29 '12 at 7:26
add comment

You can use profiling tools which will tell you how many times a method is run and how long it takes for the code to be executed. For example, you can do this in the Eclipse IDE: http://www.eclipse.org/projects/project.php?id=tptp.performance

share|improve this answer
add comment

One possible way, which is more a Hack I would not recommend for production but only for profiling. The idea would be to override the java.util.HashMap class. Simply find the source code for that class, modify it with your profiling tools, then make sure it is on top of your classpath loading.

If profiling is what you need, you could use VisualVM as well to track all these calls.

share|improve this answer
add comment

This article should help you understand why it is best to observe performance not too close to the bottleneck itself

http://www.jinspired.com/site/case-study-scala-compiler-part-5 "Part of the problem here is that developers mistakenly believe that a performance measurement solution should direct them to the actual line of code that is the problem when generally the best starting point for both observation and tuning is just above the problem in the caller-callee chain providing an appropriate enclosing context to the execution."

That said you can always count if need be but meter it back up the caller chain:

http://www.jinspired.com/site/case-study-scalas-compiler-part-1 http://www.jinspired.com/site/case-study-scala-compiler-part-2

share|improve this answer
add comment

Your Answer


