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
-
4Have you tried anything? – Rohit Jain Sep 18 '13 at 16:37
-
2How many instances - in a JVM, on a server, in the whole world? – ppeterka Sep 18 '13 at 16:39
-
@ppeterka66 - In the entire universe. – Hot Licks Sep 18 '13 at 16:40
-
I tried using counter in constructor but I am looking for other way – user2784473 Sep 18 '13 at 16:40
-
@HotLicks ... and beyond... – ppeterka Sep 18 '13 at 16:40
-
"Please let me know the solution" -- You haven't stated a problem. – Hot Licks Sep 18 '13 at 16:41
-
"Are created" - do you mean how many have been created since the program started running, or how many exist at the moment? – Joni Sep 18 '13 at 16:41
-
How many instances exist at the moment? – user2784473 Sep 18 '13 at 16:47
-
Only PhantomReference, only hardcore! – Denis Tulskiy Sep 19 '13 at 15:19
4 Answers
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.
-
-
-
You beat me by 15 seconds ;-) Anyway, you might want to mention the factory pattern as well (as the typical way to deal with instance control). – Vincent van der Weele Sep 18 '13 at 16:45
-
3
-
@rolfl Nice point! You should add it as an answer. Don't want to take credit for your idea. :) You'd have my +1. – asteri Sep 18 '13 at 16:46
-
1Also call [`super.finalize()`](http://stackoverflow.com/a/11379181/995891) (within a `finally` block) – zapl Sep 18 '13 at 17:22
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)

- 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
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.

- 5,987
- 5
- 45
- 73