Report maxmium heap size for JMH benchmarks

Viewed 1406

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?

1 Answers

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

Related