Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Empty infinite loop and GC (JVM). Please explain the effect

My Empty infinity loop

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

And profiling in Java VisualVM (picture) Visual GC

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

Please explain the effect. Why?

like image 402
couatl Avatar asked Dec 13 '12 21:12

couatl


People also ask

When JVM runs garbage collector?

As long as an object is being referenced, the JVM considers it alive. Once an object is no longer referenced and therefore is not reachable by the application code, the garbage collector removes it and reclaims the unused memory.

What is full GC in Java?

Full GC is an important phase in the garbage collection process. During this full GC phase, garbage is collected from all the regions in the JVM heap (Young, Old, Perm, Metaspace). During this phase, the JVM is paused.

What is heap memory and garbage collection?

Java objects reside in an area called the heap. The heap is created when the JVM starts up and may increase or decrease in size while the application runs. When the heap becomes full, garbage is collected. During the garbage collection objects that are no longer used are cleared, thus making space for new objects.


1 Answers

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.

like image 116
gpeche Avatar answered Nov 03 '22 22:11

gpeche