4

My application has a lot of garbage collection and I would like to analyze that. What I want to see is which objects are being garbage collected. I think that will give me idea where to look for optimization (adding cache or whatever).

Is there an option to print detailed GC information, including how many objects from each class were garbage collected?

I'm using the G1GC, if that's important.

Thanks!

duduamar
  • 3,816
  • 7
  • 35
  • 54

5 Answers5

2

Oracle's JFR (Java Flight Recorder, or Java Mission Control) is a great tool to help in this task - it shows the load on GC per object - meaning how many objects from each class are generated (this goes together with the objects that are collected). Very recommended.

duduamar
  • 3,816
  • 7
  • 35
  • 54
0

This has been answer there : Howto print java class garbage collection events?

You need to run your JVM using the arguments

-verbose:gc -XX:+PrintGCTimeStamps -XX:+PrintGCDetails
Community
  • 1
  • 1
  • I don't class names in the log.. I see details about how many total objects were collected, but I don't find the break to classes. – duduamar Nov 18 '14 at 08:22
0

I am not sure about G1GC but usually I found the VisualVM toolkit quite helpful. It's a decent combination of good feature, adequate in-depth object analysis as well as good speed (a lot of GC tools in the early age sucks on this). It's a free tool, too.

http://visualvm.java.net/download.html

Alex Suo
  • 2,977
  • 1
  • 14
  • 22
  • It's indeed a nice tool, and I've also installed the Visual GC plugin in it, but I still don't see what I want - which objects (from which classes) are being garbage collected. – duduamar Nov 18 '14 at 08:51
  • On profiler page of the process, Choose memory profile, do a snapshot, and you could analyze in depth. Also you could see what temp objects are created most by the object jumping up and down when you sort by memory usage, before and after a GC. If you want "from which class it's allocated", I am afraid none of the existing commercial tool has such handy feature. – Alex Suo Nov 18 '14 at 09:02
0

To trace Garbage Collection activity use this command:

-verbose:gc -XX:+PrintGCTimeStamps -XX:+PrintGCDetails
Maqsud
  • 1
  • 1
  • I don't class names in the log.. I see details about how many total objects were collected, but I don't find the break to classes – duduamar Nov 18 '14 at 08:23
0

I would approach this slightly differently - it isn't so much you want to know which objects are being collected, but which objects are taking up the heap (but have been collected after the next GC).

There are a number of tools that allow you inspect the objects and classes the come from in the heap, VisualVM as mentioned by Alex Suo above, JProfiler (which is nice but a paid for application), YourKit (the same) or jmap. Using any of these I would record heap state at regular intervals that you can cross-reference with your verbose garbage-collection logs.

Depending on your application, you can also make considerable progress using your verbose garbage-collection logs in conjunction with your application's activity logs (what activities cause noticeable jumps in the heap, particularly where there is some similar decrease when a GC occurs later). Even if you use the tools above, your activity logs may be crucial in identifying where the memory usage is coming from.

matt freake
  • 4,877
  • 4
  • 27
  • 56