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

查看:595
本文介绍了如何在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飞行记录器     从构建56开始的JDK 7的一部分。需要商业广告生产中使用的许可

  • VisualVM                  &n bsp;     免费/受欢迎的第三方

  • 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        memory map

  • jstat          JVM统计信息监控

  • jhat          堆分析工具

  • jdb           debugger

  • < a href =http://docs.oracle.com/javase/7/docs/technotes/tools/share/jinfo.html\"rel =nofollow noreferrer> 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天全站免登陆