Report maxmium heap size for JMH benchmarks
Asked Answered
C

1

5

I'm using the Java Measurement Harness (JMH) to benchmark some routines. I'm interested in getting the maximum heap size of each run. The JMH's GC Profiler gives me information like allocation rate and churn rate, but I'm looking for the largest the heap got during a test run. Can this be done?

Condenser answered 11/8, 2017 at 19:47 Comment(4)
you want to know the occupied heap during a single run of a method? Or the entire benchmark?Smote
Single @Benchmark method runCondenser
that's what Im saying... a @Benchmark is invoked many times. You want a single method invocation or the entire (all invocations) of the @Benchmark?Smote
I want to add a profiler that reports heap size as part of the report generation.Condenser
I
7

You can implement your own 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;
    }
}

And add it to your OptionsBuilder:

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

With the option AggregationPolicy.MAX added to the ScalarResult, the output will be the maximum result of each benchmark executed.

By the way, if you want to do metrics with memory, one good article you can read is https://cruftex.net/2017/03/28/The-6-Memory-Metrics-You-Should-Track-in-Your-Java-Benchmarks.html

Irrespirable answered 9/11, 2017 at 14:25 Comment(1)
I did write my own profiler. My profiler is very naive. It samples the heap size every few seconds using using the "totalMemory() method. Thanks for the link to the article!Condenser

© 2022 - 2024 — McMap. All rights reserved.