insitu
insitu

Reputation: 4698

Programmatic notification of JVM's GC events

I would like to track how much time is spent in GC and how much memory has been collected, but not by analysing GC logs (ie. analyzing what I got from -XX:+PrintGCWhatever).

I found that I can use Sun's ManagementFactory to get a GarbageCollectorMXBean that can give me some GCInfo object containing memory information but I have no guarantee I can collect all GC through this mean.

Does anybody know of a way to do this in code?

Upvotes: 7

Views: 1208

Answers (2)

Peter Lawrey
Peter Lawrey

Reputation: 533492

Given the range of tools to obtain this information, you are better off using those to analysis your GC behaviour and to optimise for it. e.g. using a memory profiler.

The problem you have is that the information isn't very useful unless it points to a solution to a problem. (Which you can then fix)

Upvotes: 0

NPE
NPE

Reputation: 500277

GarbageCollectorMXBean is the best I've been able to find on the Sun JVM. In my experience, it actually comes pretty close to what you're asking for.

I imagine you could have a dedicated thread that would wake up from time to time to get the GC stats. This would add some determinism to when the stats are collected.

Upvotes: 4

Related Questions