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?
Report maxmium heap size for JMH benchmarks
Asked Answered
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
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.
@Benchmark
is invoked many times. You want a single method invocation or the entire (all invocations) of the@Benchmark
? – Smote