Search code examples
javaperformancegarbage-collectionheap-memory

How to find which objects are creating the most garbage


I have an application with about 80 instances of 80 different classes. The amount of garbage generated by some subset of these classes is unacceptable and stop the world pauses are too long, since my application is real time.

What I want to find is which classes are responsible for creating the largest number of objects on heap (not aggregate size, but raw number of objects), since this is what causes stop the world pauses to take so long.

How do I find this out?

If JVisualVM is required, I have this.


Solution

  • A very lightweight approach to look at the number of objects per class is Class Histogram.

    Just generate class histogram by jmap -histo <PID>.

    Use -XX:+PrintClassHistogramBeforeFullGC, -XX:+PrintClassHistogramAfterFullGC to see class histograms when your stop-the-world pause occurs. You can compare the snapshot before/after to see which class instances get collected during the stop-the-world pause.

    See more details in Profiling number of garbage-collected object instances per class.