tags:

views:

90

answers:

2

How to get all objects present in memory?

public static void main( String[] arg ){
    MyClass object1 = new MyClass();
    BigDecimal bd = new BigDecimal();
    Object obj = new Object();
    List allObjects = getAllObjects(); // return  object1, bd, obj        
    ;
    ;
    ;  

}
+2  A: 

Probably the easiest is getting a heap dump by VisualVM. JDK also includes related tools, as the jmap tool.

Dimitris Andreou
+2  A: 

I fear there is no easy answer to do this during runtime, either you use Instrumentation http://stackoverflow.com/questions/1384228/how-can-i-access-java-heap-objects-without-a-reference or write an agent http://java.sun.com/j2se/1.5.0/docs/guide/jvmti/jvmti.html

stacker