aspose file tools*
The moose likes Java in General and the fly likes Counting the Number of Instances Created Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Counting the Number of Instances Created" Watch "Counting the Number of Instances Created" New topic
Author

Counting the Number of Instances Created

Alec Lee
Ranch Hand

Joined: Jan 28, 2004
Posts: 569
Is it possible to count the number of instances created for a class without having each instance to cooperate and tell some global/static data that it has been created. That is, can we count the number of instances and optionally get hold of a reference to it from a 3rd party class. For example, profilers can in general count the number of instances created from a class and display the fields values within each instance. How do profilers achieve this?
Pradeep Kumar
Ranch Hand

Joined: Sep 08, 2006
Posts: 108
Yes you can count the number of instance created and also control them externally. One way of doing that is by creating the instances and registering them in the MBeanServer and monitoring them using jconsole. You need to do some backgroung work to make the Class an MBean. i.e the class whose instances you need to monitor. Have a look at the JMX specification. I would suggest you for using StandardMBean for monitoring for your use.

Hope this helps you :-)


Thanks and Regards, Pradeep Kumar
SCJP 1.6, SCWCD 5.0
Alec Lee
Ranch Hand

Joined: Jan 28, 2004
Posts: 569
Hi Pradeep,

What about if the class was not created by the one doing the monitoring . For example, in profilers like VisualVM, we can attach it to any running jvm and monitor any class instance in it without having to first modify the classes to implement MBean (it seems to me that a JMX Manageable Resource have to be a class implement some JMX specific intefaces, right?).
John Pradeep.v
Ranch Hand

Joined: Jul 21, 2008
Posts: 59
Hi Alec,
I think you can achieve this using AOP (AspectJ load time weaving) to point cut any object creation and expose the statistics through JMX MBean.

you can have a look at AspectJ load time weaving for better idea.

John
Pradeep Kumar
Ranch Hand

Joined: Sep 08, 2006
Posts: 108
John Pradeep.v wrote:Hi Alec,
I think you can achieve this using AOP (AspectJ load time weaving) to point cut any object creation and expose the statistics through JMX MBean.

you can have a look at AspectJ load time weaving for better idea.

John

Hi John nice to see you here. I just had a chat with you in the evening. :-)

Hi Alec,
One more way would be to extend the class and implement the JMX interfaces and expose this class as MBean. Within the methods of the subclass just delegate it to the super class by using super. Let me know if this work out for you.

Also if you are using Spring you can go ahead and expose this as MBean as Spring can expose any bean as MBean. Advantage: you need not have to implement any JMX interfaces ;-)

Hope this help you.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Counting the Number of Instances Created
 
Similar Threads
static vs instance variables
about servlet variable
why constant are declared as static
Statics and serialization
find number of objects for a particular class and create objects accordingly?