Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 618
insitu Avatar asked Dec 04 '10 08:12

insitu


1 Answers

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.

like image 173
NPE Avatar answered Nov 08 '22 04:11

NPE