Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass JVM arguments to JMH

Tags:

jvm

sbt

jmh

I have some JMH benchmarks that I'm trying to analyze. I want to enable GC logging to see how much garbage is being generated, but I can't figure out how to pass JVM arguments. I know JMH runs benchmarks in a forked JVM, so it's not immediately obvious to me how to do this. I'm using SBT.

like image 401
kelloti Avatar asked Mar 29 '16 19:03

kelloti


People also ask

What is JMH Java?

JMH is a Java harness for building, running, and analysing nano/micro/milli/macro benchmarks written in Java and other languages targeting the JVM.

How do you run JMH?

There are two ways to run the JMH benchmark, uses Maven or run it via a JMH Runner class directly. 3.1 Maven, package it as a JAR and run it via org. openjdk. jmh.

What is JMH fork?

@Fork annotation, instructs how benchmark execution will happen the value parameter controls how many times the benchmark will be executed, and the warmup parameter controls how many times a benchmark will dry run before results are collected.


2 Answers

If I read sbt-jmh docs right, it passes the application options to JMH runner with jmh:run .... So, having that JMH command line accepts --jvmArgs "...", I would try to do jmh:run --jvmArgs "-XX:+PrintGCDetails". Or, as @apangin mentions, add @Fork(jvmArgsAppend = "-XX:+PrintGCDetails").

But for your particular use case -- "see how much garbage is generated" -- it might be even better idea to use a bundled GC profiler, activated with -prof gc. See the example at JMHSample_35_Profilers.java#l71.

like image 175
Aleksey Shipilev Avatar answered Sep 19 '22 09:09

Aleksey Shipilev


Use @Fork annotation:

@Benchmark
@Fork(jvmArgsAppend = "-XX:+PrintGCDetails")
public void someBenchmark() {
    ...
}

Note that JVM arguments passed to JMH are also propagated to forked benchmarks.

like image 40
apangin Avatar answered Sep 18 '22 09:09

apangin