How do I trace methods calls in Java?

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