我正在使用 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/