如何使用 Jconsole 计算吞吐量

How to calculate throughput with Jconsole

有没有一种方法可以使用 JConsole 接口计算 Java 程序给定时间段的垃圾收集吞吐量?例如,我想比较程序中的不同功能以及这些功能之间的 GC 吞吐量差异。

我已阅读 Oracle JConsole 文档,我能找到的最接近的指标是 GC 时间,即:

"GC time: the cumulative time spent on garbage collection and the total number of invocations. It may have multiple rows, each of which represents one garbage collector algorithm used in the Java VM."

来源:https://docs.oracle.com/javase/8/docs/technotes/guides/management/jconsole.html

但这并没有指定开始累积的开始时间。如果我能确定何时开始,我就可以自己计算吞吐量。

或者还有其他方法可以计算 JConsole 的吞吐量吗?

我一般用两个:

  1. GCViewer
  2. gceasy

同时,因为不同的工具可能会显示不同的结果。

对于你的问题的答案 - 从应用程序开始就启用 GC 日志。所以当你看到 GC 时间时,你可以得到 throughput = sum(GC time)/Total time.

By GC logs enabled, do you mean using -verbose:gc or -Xloggc: ?

不,我是说内部 counters。澄清一下就好了。

Also: do you know what GC time represents: the time from when the application was started, or the time per a given time period?

从申请开始。使用 jstat 查看 GC 时间的另一种方法。让我们有这个程序:

public class JStatTest {

    public static void main(String[] args) throws Exception {
        for (int i = 0; i < 10; ++i) {
            System.gc();
            System.out.println("Wait...");
            Thread.sleep(1000);
        }
        Thread.sleep(100_000);
    }

}

我们可以连接 jstat(从开始)和 jconsole(接近结束,循环后)并查看:

这没有启用 GC 详细日志记录。

查看总时间: