tags:

views:

455

answers:

5

How can I find the number of live objects on the heap in Java program?

A: 

As far as I know, you cannot. You can, however, get the amount of memory used for the program:

 Runtime rt = Runtime.getRuntime();
 System.out.println("Used: " + (rt.totalMemory() - rt.freeMemory());
 System.out.println("Free: " + rt.freeMemory());
 System.out.println("Total: " + rt.totalMemory());
Scharrels
A: 

If all your objects are created using some kind of Factory class you can find number of objects in the heap. Even then you have to have something in the finalize() method. Of course, this cannot be done for all objects, e.g. the jdk library classes cannot be modified. But if you want to find number of instances of a particular class you have created you can potentially find that.

fastcodejava
if i have something like String a = "abc"; a=a+"def";How will you track the number of objects through your Factory
java_geek
A: 

For debugging, you can use a profiler (like YourKit, a commercial java profiler). You'll find both open source and commercial variants of java profilers.

For integration with your code, you might look at using "Aspect Oriented Programming" technique. AOP frameworks (e.g. AspectWerkz) let you change the class files at class load time. This will let you modify constructors to register objects to your "all-my-runtime-objects-framework".

Melv
+2  A: 

jmap is the standard java utility that you can use to capture heap dumps and statistics. I can't say what protocol is used by jmap to connect to the JVM to get this info, and it's not clear if this information is available to a program running in the JVM directly (though I'm sure the program can query it's JVM through some socket to get this information).

JVM TI is a tool interface used by C code, and it has pretty much full access to the goings on of the JVM, but it is C code and not directly available by the JVM. You could probably write a C lib and then interface with it, but there's nothing out of the box.

There are several JMX MBeans, but I don't think any of them provide an actual object count. You can get memory statistics from these though (these are what JConsole uses). Check out the java.lang.management classes.

If you want some fast (easy to implement, not necessarily a quick result as a jmap takes some time), I'd fork off a run of jmap, and simply read the resulting file.

Will Hartung
+2  A: 

There is a hack you can try:

  • create your own java.lang.Object (copy the original source)
  • count the created objects in the constructor (not called for arrays)
  • add the path to your classfile to the boot classpath

see this (old) article for a sample.

Probably there are better ways to do it using JPDA or JMX, but I've not found how...

Carlos Heuberger
1) Beware: this hack *could* destabilize your JVM. 2) I don't think it will count objects "created" by deserializing a serialized object stream. (Default deserialization bypasses object constructors.)
Stephen C