Java’s built-in garbage collection logging provides a quick, easy, and free way to profile or troubleshoot your Java application.  It can help you understand your application in terms of:

  • memory usage (object count and size)
  • heap size (initial and over time)
  • GC metrics (frequency, pause time, and amount freed)

So, now you can answer questions like “how much memory is my app using,” “how much time is being spent doing garbage collection,” and “how big is the heap over time.”

For help analyzing and visualizing a GC log file, be sure to check out GCViewer, a garbage collection and heap analysis tool.

GC logging is enabled using JVM arguments; below are the arguments I use. (Note: the log file specified as file is reset each time the VM starts.)

-verbose:gc -Xloggc:file

Here’s an example GC log file which shows three GC events including one full GC.

[GC 19062K->7172K(60800K), 0.0087190 secs]
[GC 9346K->7525K(60800K), 0.0052810 secs]
[Full GC 7525K->7477K(60800K), 0.0615190 secs]

In the first event, garbage collection takes just under nine thousandths of a second to collect 11,890K memory used by objects (19,062K to 7,172K) while the heap is 60,800K.  The last event is a full GC lasting just over six hundredths of a second to free up 48K with no change in the heap.

Data like this can be invaluable in saving one’s sanity or ending arguments! For further details on garbage collection, see Sun’s GC Portal and Virtual Machine Garbage Collection Tuning documentation.

  9 Responses to “Java garbage collection logging”

  1. Hi Mike! You rock! I was looking for something to show a colleague that would explain the built-in gc log and found you when I googled. Nice work man!

  2. Hi Mike! I like the way you keep it simple. Maybe you can mention the flag to add timestamps too: -XX:+PrintGCTimeStamps

  3. Hi Mike !! Great Job !! Simple and useful !! Thanks !!

  4. Link isnt working:(.
    Can you please fix it Mike, I am eager to try it!

  5. Sorry, Girish, to which link were you referring?

  6. Simple and Concise info I can use and understand… A newbie.
    Good stuff Mike… Keep Rocking
    Cheers!

  7. Hello Mike.
    I wrote simple program.
    and use next command line to run
    java -jar “JavaApplication1.jar” -verbose:gc -Xloggc:borisLog.log
    but Log is empty :( Could You help me, please.

    package javaapplication1;
    public class Main {
    public static void main(String[] args) throws InterruptedException {
    System.out.println(“Start!”);
    for(int i = 0; i < 10; i++) {
    Thread.sleep(1000);
    System.out.println("Continue!");
    }
    System.out.println("Finish!");
    }
    }

  8. Boris, what happens if you print out the args? I think you may discover something interesting!

  9. Boris, Mike, I think the file is empty simply because there has been no garbage collection during the runtime of your application.

    If you add an explicit call to System.gc(); you should see something in the file.

 Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">

   
© 2011 And now here's something… Suffusion theme by Sayontan Sinha