How do I prevent JMH from initializing non-JMH-related classes on every benchmark?

Henri Tremblay henri.tremblay at gmail.com
Fri Dec 3 02:59:36 UTC 2021


If you do something like this, it will be instantiated only once per fork

@State(Scope.Benchmark)
public class ABench {

    private final MyInstance instance = new MyInstance();

    @Benchmark
    public void nop() {

    }
}




On Thu, 2 Dec 2021 at 07:43, Timmae219 <timerdar at googlemail.com> wrote:

> Hello JMH devs,
>
> I don’t know if this mailing list is meant to handle questions like these,
> but I think the chances to get help is bigger here than anywhere else (e.g.
> Stack Overflow). If not, please tell me that.
>
> I am currently working on a very special project which uses also JMH. The
> program should monitor the energy consumption of the System with the Intel
> Power Gadget during a JMH Benchmark. During analyzing some other
> concurrency problems I found out that my PowerGadgetProfiler class, which
> has only static members, has been initialized every time a new benchmark
> has begun. But I want it to be initialized just once in the whole program
> (at the beginning).
>
> I tried to find an annotation in JMH which does that and I also tried to
> investigate the JMH-core code to solve my problem but I wasn’t able to. I
> hope you can help me.
>
> Best regards,
> Tim Erdar
>
>


More information about the jmh-dev mailing list