Monitoring JVM Garbage Collection

I had an interesting thought in a meeting today. We were discussing various performance and monitoring improvements and monitoring garbage collection came up. It occurred to me that the JVM is able to monitor heap size and report it at any time, but the state of the last garbage collection isn’t available as far as I know from within the JVM.

You could take the round about way and log the GC status to a file and then read in that file, parse it, and get the information that you need. But you can’t get to it from within the JVM. For the reporting that we’re looking to do it would be nice to know what type of GC was last done, what the memory stats of that GC were, and how long the GC took.