How can I count objects of some type in J2me

I need to know how many objects of any type exist on my system at any given time. Standard method with static variable and and increment / decrement in constructor / distructor doesn't work because it missed Object.finalize method.

+2


source to share


4 answers


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.

+1


source


You cannot use any of these reflection materials because it is not J2me support.



The only bet would be to enable the profiler under WTK / bin / prefs. there is also a memory monitor.

+2


source


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.

0


source


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)

0


source







All Articles