How do I find out how many instances of a class currently exist?

John Mitchell

In general, you can't. Basically, things like the possibility of a variety of classloaders pretty much nix any real hope.

A number of people have asked basically this same question but, when probed, it seems that they really have some other real need and this is just the easy solution that they thought of.

For instance, if you want to know about how many instances of a class that you have written exist at runtime (in the classloader that your class was loaded from/within) then you could use a simple variant of the Singleton pattern which just counts the number of instances that it creates (rather than limiting the number created to just one).