tags:

views:

51

answers:

4

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

A: 

In standard Java, you could use a WeakReference but that's not available in J2ME. Also, there is no standard API to enumerate all objects. The "easiest" way to do that in Java would be to write a garbage collector.

So your only choices are to run the code in a Java IDE which can do profiling at runtime or handle the destruction of your objects manually, so you can count them.

Aaron Digulla
A: 

If you need it at runtime and want to process it programmatically as part of your app it's quite hard - otherwise you could create heapdumps and analyse them. then you'll know how many instances of which objects are currently used (i.e. on the heap)

msparer
+1  A: 

As a brutforce answer you can produce all your objects with special singleton factory, where you can increment count, when new object is produced, and delete them through 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 do not give you exactly results, but something roundly.

St.Shadow
+2  A: 

You can't use any of this reflection stuff because it isn't support by J2me.

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

drubin
Profiler Doesn't work in Samsung SDK
Pirks
Is there a reason you need to use the Samsung SDK? You could profile your app through the standard WTK you should receive similar results. Obviously not 100% compatible as far as exact time/processing data, but I wouldn't trust an emulator either any way.
drubin