java - 报告 JMH 基准测试的最大堆大小

标签 java benchmarking heap-memory jmh

我正在使用 Java Measurement Harness (JMH) 对一些例程进行基准测试。我有兴趣获得每次运行的最大堆大小。 JMH 的 GC Profiler 为我提供了分配率和流失率等信息,但我正在寻找测试运行期间获得的最大堆。这可以做到吗?

最佳答案

您可以实现自己的分析器:

public class MaxMemoryProfiler implements InternalProfiler {

    @Override
    public String getDescription() {
        return "Max memory heap profiler";
    }

    @Override
    public void beforeIteration(BenchmarkParams benchmarkParams, IterationParams iterationParams) {

    }

    @Override
    public Collection<? extends Result> afterIteration(BenchmarkParams benchmarkParams, IterationParams iterationParams,
        IterationResult result) {

        long totalHeap = Runtime.getRuntime().totalMemory(); // Here the value
                                                         // you want to
                                                         // collect

        Collection<ScalarResult> results = new ArrayList<>();
        results.add(new ScalarResult("Max memory heap", totalHeap, "bytes", AggregationPolicy.MAX));

        return results;
    }
}

并将其添加到您的 OptionsBuilder 中:

    OptionsBuilder()
         /* options ... */               
        .addProfiler(MaxMemoryProfiler.class);

将选项 AggregationPolicy.MAX 添加到 ScalarResult 后,输出将是执行的每个基准测试的最大结果。

顺便说一下,如果你想用内存做指标,你可以阅读一篇很好的文章是https://cruftex.net/2017/03/28/The-6-Memory-Metrics-You-Should-Track-in-Your-Java-Benchmarks.html

关于java - 报告 JMH 基准测试的最大堆大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45642782/

相关文章:

java - 如果不存在则插入整数,如果 Firebase 实时数据库 Android 中已存在则递增

java - EJB 中@Stateless 相对于@Singleton 的真实用例是什么

unit-testing - 我如何在 Go 中编写使用 -short 标志的测试,它可以与 -benchmark 标志结合使用吗?

c# - 基准测试 .Net 正则表达式选项 - 或者 RightToLeft 做什么?

java - 仅分配 1 个数组时如何计算 JVM 的最小 MaxHeapSize?

java - Maven JSP 标签库原型(prototype)

java - 函数 Guava 在单个对象上应用函数

function - 在基准测试期间在 R 中多次运行函数

javascript - 在 Chrome 中,为什么 TypedArrays 不在 JS 堆上?

c# - 不安全的 C# 代码会导致 64 位平台上的堆损坏,除非为 x86 平台构建