如何在 Java 中跟踪方法调用? [英] How do I trace methods calls in Java?

查看:21
本文介绍了如何在 Java 中跟踪方法调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑下面两个简单的 Java 类:

Consider the two simple Java classes below:

第一个例子

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();
   }
}

第二个例子

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...");
    }
}

程序运行后,如何跟踪(1)哪个对象调用哪个方法(2),调用了多少次?

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

稍微精确一点,我可能有 100 个类和 1000 个对象,每个对象调用 100 个方法.我希望能够跟踪(在我运行程序之后),哪个对象调用了哪个方法以及调用了多少次.

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.

感谢您的任何建议.

推荐答案

这会为所有线程中所有对象的每个方法调用打印一行:

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

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

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

您可以使用像   housemd 这样的呼叫跟踪器; 或   btrace  或 ;inTrace

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:

java-callgraph

(这是一篇文章关于主题)

上面已弃用的方法将被删除,因为现在有特定于 JVM 的替代方法:

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

  • Java 飞行记录器    JDK 7 的一部分,从 build 56 开始.需要商业许可才能用于生产
  • VisualVM;        免费/受欢迎的第三者
  • Java Flight Recorder     Part of JDK 7 as of build 56. Requires commercial license for use in production
  • VisualVM                       Free/popular 3rd party

这两个工具都非常容易设置和开始收集信息,并且具有漂亮的 GUI 界面.它们附加到正在运行的 JVM 进程并允许线程快照和各种其他类型的诊断(Visual VM 有很多可用的插件,但如果您想超越默认行为,则可能需要一段时间来整理以进行配置和理解,而默认情况下,JFR 会检测更多).

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).

此外,不要低估 JVM 分布式命令行实用程序 ($JAVA_HOME/bin) 在执行一些易于访问的诊断方面的用处.

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

  • jstack     堆栈跟踪
  • jmap      内存映射
  • jstat      JVM 统计监控
  • jhat       堆分析工具
  • jdb        调试器
  • jinfo       java 进程或核心文件配置信息
  • jstack      stack trace
  • jmap       memory map
  • jstat        JVM statistics monitoring
  • jhat         heap analysis tool
  • jdb          debugger
  • jinfo        java process or core file config info

这篇关于如何在 Java 中跟踪方法调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆