1 2 Previous Next 17 Replies Latest reply on Jan 21, 2004 10:03 AM by 807582

    number of instances

      How can I get the number of instances (or the list of instances) of a class ?
        • 1. Re: number of instances

          try this,

          Class c[] = t.getClass().getDeclaredClasses();

          where t is instance of test class...


          • 2. Re: number of instances
            • 3. Re: number of instances
              from API :

              public Class[] getDeclaredClasses()

              Returns an array of Class objects reflecting all the classes and interfaces declared as members of the class represented by this Class object. This includes public, protected, default
              (package) access, and private classes and interfaces declared by the class, but excludes inherited classes and interfaces. This method returns an array of length 0 if the class
              declares no classes or interfaces as members, or if this Class object represents a primitive type, an array class, or void.
              • 4. Re: number of instances
                Hello Olivier,

                the short answer is: you can't.

                Other options are:
                - Get your hands on a profiling tool.
                - DO NOT DO THIS AT HOME: create your own version of java.lang.Object, and add some logging routines to its constructor. Beware of legal issues with this one!
                - Implement a JPDA solution.

                Good luck,

                Manuel Amago.
                • 5. Re: number of instances
                  If this is a class you wrote, then include a static ArrayList variable, and in every constructor add "this" to that variable. You will then have a list of all instances of the class. This has the unfortunate side effect that those instances can never be garbage-collected, so if that is a problem you will want to use weak, soft, or phantom references -- I don't know which would be best.
                  • 6. Re: number of instances
                    normaly, we add classes to a list or a vector, if we want to have references to multiple instantiated classes later.

                    if you want to check how many classes of type X an application instantiate so just use the debugger interface (com.sun.jdi - see class BootStrap to begin)

                    if you want to have a list of all classes in your applikation, just use a self defined classloader.

                    • 7. Re: number of instances
                      If you just want to know how much instances created for a user defined class then i think this would be a solution

                      Just declare a static variable in your class and in the constructor increment that variable by one.In this way you can count how much instances created for a class.

                      try this
                      • 8. Re: number of instances
                        A static counter won't work 'cause it'll only tell you how many object have been created; not how many are currently being referenced.

                        Decerasing the counter on finalized won't be completely accurate either.
                        • 9. Re: number of instances
                          Hi Marnagi,

                          It will be very much helpful if you could provide some additional details of implementing the JPDA solution. It looks to be a much better solution.

                          • 10. Re: number of instances
                            Hi Oliver,
                                 how you can see, don't is a simple task, I don't know a fast solution, but:
                            The main problem, is that objects don't have destroy() method, then you don't know when instance a object is destroyed, neither know when will be garbage collected.

                            a) If you store all new object instance in a collection, array... (hold instance reference):
                                 1) All instances never be garbage-collected.
                                 2) Only can add instances, never decrease instance number (don't know when instance destroy).
                            b) If use a instance counter
                                 1) Same problem, only can add to counter, althoug garbage collector will destroy not referenced instances.
                            Are you sure what don't have other way for solve your problem ?, this is hard work ! I may be will try find other solution !
                            • 11. Re: number of instances

                              public class YourClass {
                              private final static Object mutex = new Object();
                              public final static int numberOfInstances = 0;

                              public YourClass() {
                              synchronize(mutex) {

                              protected void finalize() throws Throwable {
                              synchronize(mutex) {
                              • 12. Re: number of instances
                                protected void finalize() throws Throwable {
                                     synchronize(mutex) {
                                elquebuscamascosas, your idea is good, but who and when inco will call the finalize method ???

                                • 13. Re: number of instances
                                  See Javadoc:

                                  * Called by the garbage collector on an object when garbage collection
                                  * determines that there are no more references to the object.
                                  * A subclass overrides the <code>finalize</code> method to dispose of
                                  * system resources or to perform other cleanup.
                                  * <p>
                                  * The general contract of <tt>finalize</tt> is that it is invoked
                                  * if and when the Java<font size="-2"><sup>TM</sup></font> virtual
                                  * machine has determined that there is no longer any
                                  * means by which this object can be accessed by any thread that has
                                  * not yet died, except as a result of an action taken by the
                                  * finalization of some other object or class which is ready to be
                                  * finalized. The <tt>finalize</tt> method may take any action, including
                                  * making this object available again to other threads; the usual purpose
                                  * of <tt>finalize</tt>, however, is to perform cleanup actions before
                                  * the object is irrevocably discarded. For example, the finalize method
                                  * for an object that represents an input/output connection might perform
                                  * explicit I/O transactions to break the connection before the object is
                                  * permanently discarded.
                                  * <p>
                                  * The <tt>finalize</tt> method of class <tt>Object</tt> performs no
                                  * special action; it simply returns normally. Subclasses of
                                  * <tt>Object</tt> may override this definition.
                                  * <p>
                                  * The Java programming language does not guarantee which thread will
                                  * invoke the <tt>finalize</tt> method for any given object. It is
                                  * guaranteed, however, that the thread that invokes finalize will not
                                  * be holding any user-visible synchronization locks when finalize is
                                  * invoked. If an uncaught exception is thrown by the finalize method,
                                  * the exception is ignored and finalization of that object terminates.
                                  * <p>
                                  * After the <tt>finalize</tt> method has been invoked for an object, no
                                  * further action is taken until the Java virtual machine has again
                                  * determined that there is no longer any means by which this object can
                                  * be accessed by any thread that has not yet died, including possible
                                  * actions by other objects or classes which are ready to be finalized,
                                  * at which point the object may be discarded.
                                  * <p>
                                  * The <tt>finalize</tt> method is never invoked more than once by a Java
                                  * virtual machine for any given object.
                                  * <p>
                                  * Any exception thrown by the <code>finalize</code> method causes
                                  * the finalization of this object to be halted, but is otherwise
                                  * ignored.
                                  * @throws Throwable the <code>Exception</code> raised by this method
                                  protected void finalize() throws Throwable { }
                                  • 14. Re: number of instances
                                    Ok, you have al reason of world !... your code is very good !, I don't remember finalize() method of java.lang.Object Class.
                                    1 2 Previous Next