Empty infinite loop and GC (JVM). Please explain t

2019-06-18 07:48发布

问题:

My Empty infinity loop

    public static void main(String[] args) {
        while (true) {}
    }

And profiling in Java VisualVM (picture)

As you can see, I do not create objects. Why change a heap?

Please explain the effect. Why?

回答1:

Basically any Java application is multithreaded, the fact that your main thread does not allocate memory does not mean that the others do not allocate either. In fact it is very likely that by attaching via VisualVM and showing the GC tab you have spawned some threads in the VM to monitor GC resources and feed VisualVM the metrics that become those shiny charts. And that monitoring will likely allocate some resources of its own to do its job.