Question

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 .

Was it helpful?

Solution 2

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 WeakReferences. 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).

OTHER TIPS

I guess you could do it in theory. The (shallow) size of an object is easy to calculate from the number and type of the object's fields, and you could use the ASM instrumentation to insert code into to each constructor to record the size (somewhere) each time an object is constructed.

Dealing with arrays would be tricky. Array allocation doesn't entail a constructor, so you would need to instrument all of the places where arrays are created. Since some of those places could be in native code, it is hard to see how you would do this.

Another tricky case would be dealing with things that create objects without calling their constructors; e.g. Object Serialization and native cloning.

So all in all, this is probably NOT a practical approach.


The other point is that anything that uses ASM and similar stuff is arguably not "Pure Java" ... unless the usage is actually part of the JVM itself.

Use the Runtime class

    long MEGABYTE = 1024L * 1024L;
    Runtime runtime = Runtime.getRuntime();
    runtime.gc();
    System.out.println("Total Memory : " + runtime.totalMemory()/MEGABYTE);
    System.out.println("Used Memory  : " + (runtime.totalMemory() - runtime.freeMemory()) / MEGABYTE);
    System.out.println("Free Memory  : " + runtime.freeMemory() / MEGABYTE);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top