Search code examples
javagarbage-collectionjconsolethroughput

How to calculate throughput with Jconsole


Is there a way to calculate the Garbage Collection throughput for a given time slot of a Java program using the JConsole interface? For example, I want to compare different functionalities in a program and how GC throughput varies between these.

I have read the Oracle JConsole docs and the closest metric I can find is GC Time which is:

"GC time: the cumulative time spent on garbage collection and the total number of invocations. It may have multiple rows, each of which represents one garbage collector algorithm used in the Java VM."

Source: https://docs.oracle.com/javase/8/docs/technotes/guides/management/jconsole.html

But this doesn't specify a start time for when it starts accumulating. If I could identify when this starts I could calculate throughput myself.

Or alternatively is there any other way to calculate throughput on JConsole?


Solution

  • I usually use two:

    1. GCViewer
    2. gceasy

    simultaneously, because different tools could show different results.

    For your question the answer - GC logs enabled from the start of application. So when you see GC time, you could get throughput = sum(GC time)/Total time.

    By GC logs enabled, do you mean using -verbose:gc or -Xloggc: ?

    No, I mean internal counters. It is good to clarify.

    Also: do you know what GC time represents: the time from when the application was started, or the time per a given time period?

    From start of application. Another approach to see GC time using jstat. Let would we have this program:

    public class JStatTest {
    
        public static void main(String[] args) throws Exception {
            for (int i = 0; i < 10; ++i) {
                System.gc();
                System.out.println("Wait...");
                Thread.sleep(1000);
            }
            Thread.sleep(100_000);
        }
    
    }
    

    We can connect with jstat (from start) and jconsole (near to end, after cycle) and see this:

    enter image description here

    This is without enabled GC verbose logging.

    To see total time:

    enter image description here