Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to log if a class in the JVM is used?

Tags:

java

jvm

I have a Tomcat with some applications running. I cannot restart the Tomcat but I would like to monitor the usage of class files.

I would like to log if a specified class is used. Is this possible? How could I accomplish it?

Class Usage: If an object for this class is instantiated or methods are called etc.

Clarification: I cannot restart the application. Every solution with recompiling the running code are not acceptable. That makes the problem so hard.

Remote debugging/JMX is not enabled yet. It would be a similar effort like recompiling the application to activate it.

Platform is RHEL, 64 Bit.

like image 383
guerda Avatar asked Nov 26 '22 20:11

guerda


2 Answers

I would like to log if a specified class is used; i.e. if an object for this class is instantiated or methods are called etc.

A memory profiler would tell you if a reachable instance of a class exists at the instant you run the profiler. An execution profiler could tell you that a method or constructor is called during some interval ... though it might also miss a call, due to the way that profilers work.

The webapp's classloader could in theory tell you if a class has been loaded, but I doubt there is a way to call the classloader's method that doesn't involve a restart. Also, there is no way to know if a method has EVER been called or an instance has EVER been created apart from adding monitoring hooks to the class. And adding those hooks would entail a restart.

And of course there are other ways that a class could be "used" that don't entail constructing instances or calling its methods.

So depending on what you are really trying to figure out, you may be out of luck.

like image 109
Stephen C Avatar answered Dec 05 '22 12:12

Stephen C


I think you need profiling for that. Profiler will allow you to see which classes are used. Or the programmers fav - System.out.println :)

like image 35
Padmarag Avatar answered Dec 05 '22 11:12

Padmarag