I was fiddling with Java heap for sometime. Using JVMTI , we can keep track of java heap.
But is there any way to achieve the same using pure java ?
I am using java instrumentation and asm framework for class transformation in runtime.
My objective is to keep track of every single object created , which means I need to check their sizes periodically. I can check if new object is created using asm. What I need is to check the sizes of the object created from the heap space.
Any pointers in this regard ?
edited on 28th December 2013
Ok , I was able to do something from ASM though it might not be the best possible solution ( best way is JVMTI that I completely agree ).
I had to override visitVarInsn (for local variables) , visitFieldInsn ( for instance and class variable) and visitTypeInsn (for NEW and NEWARRAY objects getting created )
Every time a new object gets created , I record the object ( yeah , this is not a good way I know , and this may cause resource leak. If you have any better solution , please tell me . I need it so badly [:( ] ) And I periodically check the object size.
Can you please provide me with better options ? (As Stephen has rightly pointed out , this object recording is definitely not going to work , but I am not comfortable with JVMTIeither [:(]). Also , pardon my ignorance , I could not find suitable MXBean methods.
MemoryMXBean has getHeapMemoryUsage() , but I can not track individual object size from this method. May be I should write my own MBean .