Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I trace methods calls in Java?

Consider the two simple Java classes below:

First Example

class Computer {
    Computer() {
        System.out.println("Constructor of Computer class.");
    }
    void On() {
        System.out.println("PC turning on...");
    }
    void working() {
        System.out.println("PC working...");
    }
    void Off() {
        System.out.println("PC shuting down...");
    }
    public static void main(String[] args) {
        Computer my = new Computer();
        Laptop your = new Laptop();
        my.On();
        my.working();
        your.On();
        your.working();
        my.Off();
        your.Off();
   }
}

Second Example

class Laptop {
    Laptop() {
        System.out.println("Constructor of Laptop class.");
    }
    void On() {
        System.out.println("Laptop turning on...");
    }
    void working() {
        System.out.println("Laptop working...");
    }
    void Off() {
        System.out.println("Laptop shuting down...");
    }
}

After the program run, how do I trace (1) which object call which method (2) and how many times?

Just a little precision, I might have 100 classes and 1000s of objects each of them calling 100s of methods. I want to be able to trace (after I run the program), which object called which method and how many times.

Thanks for any suggestion.

like image 327
george24 Avatar asked Apr 01 '15 03:04

george24


People also ask

How do you trace in Java?

Tracing for Java Plug-in and Java Web Start can be turned on by setting the property deployment. trace property to true . This property turns on all tracing facilities inside Java Plug-in and Java Web Start. To enable more fine-grained tracing, the deployment.

What are method calls in Java?

The MethodCall operator is used to call an arbitrary Java method using the Java reflection API. Two forms of method call are supported, static and instance level. For static method calls Input 1 should be set to null and Inputs 2,3,4 are required.

How do you call a method in OOPS in Java?

To call a method in Java, write the method name followed by a set of parentheses (), followed by a semicolon ( ; ). A class must have a matching filename ( Main and Main. java).

Can you use this to call a method in Java?

The "this" keyword in Java is used as a reference to the current object, within an instance method or a constructor. Yes, you can call methods using it.


1 Answers

This prints a line for each method call of all objects in all threads:

Runtime.traceMethodCalls() (deprecated / no-op in Java 9)

And

Runtime.traceInstructions (deprecated / no-op in Java 9)

You can use a call tracer like   housemd  or   btrace  or  inTrace

For more involved analysis, you can use a call graph utility like one of these:

  • javashot

  • java-callgraph

(here is an article on the subject)

The deprecated methods above are slated for removal, because there are now JVM-specific alternatives:

  • Java Flight Recorder     Part of JDK 7 as of build 56. Requires commercial license for use in production
  • VisualVM                       Free/popular 3rd party

Both of those tools pretty easy to setup and start collecting information and have nice GUI interfaces. They attach to a running JVM process and allow for thread snapshots and various other kinds of diagnosis (Visual VM has a lot of available plugins but that can take awhile to sort through to configure and understand, if you want to go beyond default behavior, whereas JFR is instrumented with more by default).

Also, don't underestimate the usefulness of JVM distributed command line utilities ($JAVA_HOME/bin), for performing some easily accessible diagnostics.

  • jstack      stack trace
  • jmap       memory map
  • jstat        JVM statistics monitoring
  • jhat         heap analysis tool
  • jdb          debugger
  • jinfo        java process or core file config info
like image 88
clearlight Avatar answered Sep 18 '22 15:09

clearlight