As a result of some memory leak detection work, I would like to be able to progrmmatically connect to a JVM and query the number of instances of a certain object that are on that JVM's heap.
The options I have considered are:
1) The Java platform MBeans/MXBeans, but there do not seem to be any attributes/operations available that would give me this information.
2) Using the HotSpotDiagnostic MBean to tell the JVM to output a heap dump, downloading the heap dump over FTP, and then parsing it for the information I require, but I'm guessing parsing a HPROF file is no easy task.
3) Instrumentation, but this appears to be more useful for analyzing behaviour of objects, rather than discovering how many instances of an object are on the heap. Perhaps it could be used to hook into the instantiation of an object, but could it be used to also hook into the garbage collection of one?
Has anyone done this before, and could they point me in the right direction? I am not looking for an end-to-end solution; I would like to learn something whilst doing it!