How can I find the number of live objects on the heap in Java program?
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.