-2

How do I know how many instances of class are created at any given time in java? I have class A and I want to know how many instances are created at any given time? Please let me know the solution

user2784473
  • 19
  • 1
  • 1

4 Answers4

7

Keep track of it in your constructor with a static variable.

public class A {

    private static int instances = 0;

    public A() {
        instances++;
    }
}

If concurrency is a concern to you:

public class A {

    private static final Object LOCK = new Object();
    private static int instances = 0;

    public A() {
        synchronized(LOCK) {
            instances++;
        }
    }
}

To address what you've said in the comments below your question: if you want to know how many currently exist (i.e., have not been garbage collected) you could try something like the following:

public class A {

    private static final Object LOCK = new Object();
    private static int instances = 0;

    public A() {
        synchronized(LOCK) {
            instances++;
        }
    }

    protected void finalize() throws Throwable {
        synchronized(LOCK) {
            instances--;
        }
    }
}

The finalize() method will be called right before the object is garbage collected. However garbage collection is notoriously unreliable.

Also, as a side note, you could use the AtomicInteger class rather than an int and the synchronized blocks, as @rolfl says below. All upvotes for that idea should go to his answer, should he post one.

Community
  • 1
  • 1
asteri
  • 11,402
  • 13
  • 60
  • 84
0

That's actually not such a bad question, you can control the number of instances by using sychronize and a counter, this design pattern is called instance-controlled class and it's one of the first things that are mentioned in "effective Java" by Joshua Bloch (chapter 2)

Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
  • Of course, there's no way to count the instances that are collected. – Hot Licks Sep 18 '13 at 16:42
  • @HotLicks sorry but I didn't understand your comment (the collected part). – Nir Alfasi Sep 18 '13 at 16:43
  • One can attempt to subtract the garbage-collected objects with a `finalize` method, but it's highly unreliable. So the total number in existence at any one point in time is not knowable without using "instruments". – Hot Licks Sep 18 '13 at 19:58
  • @HotLicks but that's another question (how many LIVE objects are there) - the original question was how many instances were created ;) – Nir Alfasi Sep 18 '13 at 20:08
  • How many instances *ARE* created. That can be interpreted either way. And later he says "How many instances exist at the moment". – Hot Licks Sep 18 '13 at 20:31
  • @HotLicks yeps, I guess you're right - it can be interpreted either way – Nir Alfasi Sep 18 '13 at 20:37
0

There's no direct way, but if you're really interested in keeping track of this information you could create a private static int instanceCounter field and increment it in your constructor(s). You would also need a finalize method which would decrement the counter whenever an instance is garbage collected.

StormeHawke
  • 5,987
  • 5
  • 45
  • 73
0

you could implement a counter when it inits

sinfere
  • 927
  • 8
  • 9