Search code examples
javagarbage-collectionvisualvm

How do we know which type of GC(young/full) is currently being executed in my Java Program using VisualVM or any other tool?


I am doing a performance analysis of a Program using VisualVM tool. I was able to check CPU usage and heap usage of the program but couldn't figure out which type of GC (young GC/full GC) is executing currently. Is there anyway to find it using VisualVM or is there any other tool for it?


Solution

  • I am no VisualVM expert, so I do not know how to do this using. But I can provide you a couple of alternatives

    JSTAT

    You can see what kind of gc is being performed by using jstat

    First we need to discover the PID of your Java running process. For this, we can use jps. In this example, a running Eclipse IDE.

    jps
    10156 org.eclipse.equinox.launcher_1.5.700.v20200207-2156.jar
    

    Now use the following command jstat -gc 10156 1s

    1s refers to the sampling interval. You can use whatever one that fits your needs.

    This command outputs a table with a lot of columns, and I am finding it very hard to format (sorry), so please focus on these two ones

    • YGC: Number of young generation garbage collection (GC) events.
    • FGC: Number of full GC events.

    Watching these two counters will give you a general idea of what type of collections are being performed.

    JVM Flags

    Besides, you can use a JVM flag

    • -Xlog:gc* for java 11
    • -XX:+PrintGCDetails for java 8

    Which will print all GC details along the console: memory footprint before/after each collection, time spent, ergonomics ...

    Output of this flag is somehow tricky to understand as it depends on your JVM implementation, selected GC, JVM flags ...