Since you are using Instrumentation, you already have access to an API that tells you the object sizes. The very same Instrumentation
class that you use to modify the byte code of the classes provides the method getObjectSize(Object)
.
The resource leak you mention regarding your object tracking can be solved easily. Just put the objects into WeakReference
s. Then you can use a ReferenceQueue
to learn when the object has been garbage collected which is an important feature when you want to track the heap usage.
But I’m not quite sure what you want to achieve exactly.
By the way, as far as I know, JVisualVM does not use JVMTI. But JVisualVM does not track every object, either. A full heap analysis requires a heap dump. This can be done using the MXBean
with the name "com.sun.management:type=HotSpotDiagnostic"
sending the method invocation dumpHeap(String outputFile, boolean live)
.