Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 报告JMH基准的最大堆大小_Java_Benchmarking_Heap Memory_Jmh - Fatal编程技术网

Java 报告JMH基准的最大堆大小

Java 报告JMH基准的最大堆大小,java,benchmarking,heap-memory,jmh,Java,Benchmarking,Heap Memory,Jmh,我正在使用Java度量工具(JMH)对一些例程进行基准测试。我感兴趣的是获得每次运行的最大堆大小。JMH的GC分析器给我提供了分配率和搅动率等信息,但我正在寻找在测试运行期间堆得到的最大值。这能做到吗 您可以实现自己的探查器: public class MaxMemoryProfiler implements InternalProfiler { @Override public String getDescription() { return "Max mem

我正在使用Java度量工具(JMH)对一些例程进行基准测试。我感兴趣的是获得每次运行的最大堆大小。JMH的GC分析器给我提供了分配率和搅动率等信息,但我正在寻找在测试运行期间堆得到的最大值。这能做到吗

您可以实现自己的探查器:

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;
    }
}
将选项
AggregationPolicy.MAX
添加到
ScalarResult
后,输出将是执行的每个基准测试的最大结果


顺便说一句,如果您想使用内存进行度量,您可以阅读的一篇好文章是

您想知道在一次方法运行期间占用的堆吗?还是整个基准测试?单个@benchmark方法运行我就是这么说的。。。
@基准
被多次调用。您想要单个方法调用还是整个(所有调用)的
@Benchmark
?我想要添加一个报告堆大小的探查器,作为报告生成的一部分。我确实编写了自己的探查器。我的剖析器很幼稚。它使用“totalMemory()方法”每隔几秒钟对堆大小进行一次采样。感谢文章的链接!
    OptionsBuilder()
         /* options ... */               
        .addProfiler(MaxMemoryProfiler.class);