How can I count objects of some type in J2me
As an answer to brutforce, you can create all your objects with a dedicated singleton factory where you can increment the counter when a new object is created and dispose of them via this factory to:
Object newOne = ObjectsFactory.getInstance().getNewObject(); // in this method count++
...
...
// we don`t need newOne anyMore
ObjectsFactory.getInstance().releaseObject(newOne); // here count--
newOne = null; // let gc do its work.
This approach doesn't give you exact results, but it's cool.
source to share
In standard Java, you can use WeakReference
, but not available in J2ME. Also, there is no standard API for enumerating all objects. The "easiest" way to do this in Java is to write a garbage collector.
So your only choice is to run code in the Java IDE that can do profiling at runtime, or handle destroying your objects manually so you can count them.
source to share
If you need it at runtime and want to handle it programmatically as part of your application, it's pretty tricky - otherwise you could create heapdumps and parse them. then you will know how many object instances are currently in use (i.e. on the heap)
source to share