Alex Luberg
Alex Luberg

Reputation: 253

How to count how many times which class was used in runtime of Java app

I think I need to setup some profiler, or somehow configure the class loader to save this info. Here what I need: When ever a JVM runs,I need to execute a process that keep track of java files being loaded by the class loader. Across all the threads (and processes also, but that is another JVM).

So in the output I need to have information how many times which class was used.

Can you offer some tools and configuration examples for these? That's better be a command line tool & free.

Upvotes: 1

Views: 674

Answers (3)

Roger Lindsjö
Roger Lindsjö

Reputation: 11543

If you just need to see which classes were loaded you can run with

java -verbose:class com.your.Class

Upvotes: 0

Bob Kuhar
Bob Kuhar

Reputation: 11130

Profilers will certainly give you that information, but you need to run the JVM under profiling, for which there is a performance cost.

If you are planning on collecting this information on-demand from production systems, you are probably going to need to instrument the classes/packages you are interested in with JMX. Then you can use something like HP OpenView or Nagios to monitor the values. http://www.ibm.com/developerworks/java/library/j-jtp09196/index.html talks about how getting started in JMX. Perhaps the JVM already collects some of this stuff, I don't know.

The downside of the JMX approach is that you have to know beforehand what you want to collect and instrument the target classes appropriately.

Upvotes: 1

Andy
Andy

Reputation: 8949

You should use a Java Profiler. Some open source options are here.

Upvotes: 2

Related Questions