Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Measuring time spent on GC in JVM

Suppose I am testing a Java server application. I know how much time it takes to finish the test. Now I'd like to know how much was spent on GC during that test. How can I do it?

like image 771
Michael Avatar asked Dec 17 '12 13:12

Michael


People also ask

What is time spent in GC?

"% Time in GC is the percentage of elapsed time that was spent in performing a garbage collection (GC) since the last GC cycle. This counter is usually an indicator of the work done by the Garbage Collector on behalf of the application to collect and compact memory.

How do I monitor my GC?

The typical CUI GC monitoring method involves using a separate CUI application called "jstat", or selecting a JVM option called "verbosegc" when running JVM. GUI GC monitoring is done by using a separate GUI application, and three most commonly used applications would be "jconsole", "jvisualvm" and "Visual GC".

How long does Java GC take?

Depending on the time of day, full GC happens as often as every 5 minutes when busy, or up to 30 minutes can go by in between full GCs during the slow periods.

How do you check which GC algorithm is used by JVM?

For modern computer (multiple cpus, big memory), JVM will detect it as server machine, and use Parallel GC by default, unless you specify which gc to use via JVM flags explicitly.


2 Answers

I guess that when GC (Garbage Collector) is working the application stops and resumes when GC finishes

I don't think that is a safe assumption. Are you sure the garbage collector is not working in parallel with your application code?

To measure the time spent in collecting garbage you can query the Garbage Collector MXBean.

Try this:

public static void main(String[] args)  {
    System.out.println("collectionTime = " + getGarbageCollectionTime());
}

private static long getGarbageCollectionTime() {
    long collectionTime = 0;
    for (GarbageCollectorMXBean garbageCollectorMXBean : ManagementFactory.getGarbageCollectorMXBeans()) {
        collectionTime += garbageCollectorMXBean.getCollectionTime();
    }
    return collectionTime;
}
like image 200
Steve McLeod Avatar answered Oct 04 '22 17:10

Steve McLeod


The simplest way is to use the -Xloggc and -XX:-PrintGCTimeStamps options when starting up your JVM. I think it prints out how long garbage collection takes.

http://www.oracle.com/technetwork/java/javase/tech/vmoptions-jsp-140102.html

like image 33
hvgotcodes Avatar answered Oct 04 '22 19:10

hvgotcodes