How can I find the number of live objects on the heap in Java program?
-
9For what reason do you need to know the number of live heap objects? Chances are there's a better way to do what you want. – Anon. Feb 14 '10 at 20:23
-
1Do you want to get this information from within the program itself? Or using a debugging tool? – Nicolas Raoul Dec 21 '17 at 04:17
10 Answers
jmap is the standard java utility that you can use to capture heap dumps and statistics. I can't say what protocol is used by jmap to connect to the JVM to get this info, and it's not clear if this information is available to a program running in the JVM directly (though I'm sure the program can query it's JVM through some socket to get this information).
JVM TI is a tool interface used by C code, and it has pretty much full access to the goings on of the JVM, but it is C code and not directly available by the JVM. You could probably write a C lib and then interface with it, but there's nothing out of the box.
There are several JMX MBeans, but I don't think any of them provide an actual object count. You can get memory statistics from these though (these are what JConsole uses). Check out the java.lang.management classes.
If you want some fast (easy to implement, not necessarily a quick result as a jmap takes some time), I'd fork off a run of jmap, and simply read the resulting file.

- 115,893
- 19
- 128
- 203
The simplest way is to use jmap
tool. If you will print objects histogram at the end you'll see total number of instances and also accumulated size of all objects:
jmap -histo <PID>
will print whole objects with number of instances and size. The last line will contain total number
Total 2802946 174459656
Second column is total instances count, and last is total bytes.

- 13,724
- 6
- 60
- 85
Use jvisualvm, and do a memory sample. It will show the number of classes and instances:

- 2,697
- 2
- 24
- 34
-
1To clarify, jvisualvm is part of the Sun JDK (the chosen screenshot may mislead some people into believing it is part of IntelliJ). – mikewse Jun 06 '14 at 11:23
There is a hack you can try:
- create your own java.lang.Object (copy the original source)
- count the created objects in the constructor (not called for arrays)
- add the path to your classfile to the boot classpath
see this (old) article for a sample.
Probably there are better ways to do it using JPDA or JMX, but I've not found how...

- 28,957
- 10
- 64
- 87
-
31) Beware: this hack *could* destabilize your JVM. 2) I don't think it will count objects "created" by deserializing a serialized object stream. (Default deserialization bypasses object constructors.) – Stephen C Feb 15 '10 at 00:05
As far as I know, you cannot. You can, however, get the amount of memory used for the program:
Runtime rt = Runtime.getRuntime();
System.out.println("Used: " + (rt.totalMemory() - rt.freeMemory());
System.out.println("Free: " + rt.freeMemory());
System.out.println("Total: " + rt.totalMemory());

- 3,055
- 25
- 31
If all your objects are created using some kind of Factory
class you can find number of objects in the heap. Even then you have to have something in the finalize()
method. Of course, this cannot be done for all objects, e.g. the jdk library classes cannot be modified. But if you want to find number of instances of a particular class you have created you can potentially find that.

- 39,895
- 28
- 133
- 186
-
3if i have something like String a = "abc"; a=a+"def"; How will you track the number of objects through your Factory – java_geek Feb 14 '10 at 20:43
For debugging, you can use a profiler (like YourKit, a commercial java profiler). You'll find both open source and commercial variants of java profilers.
For integration with your code, you might look at using "Aspect Oriented Programming" technique. AOP frameworks (e.g. AspectWerkz) let you change the class files at class load time. This will let you modify constructors to register objects to your "all-my-runtime-objects-framework".

- 983
- 6
- 13
public class NumOfObjects {
static int count=0;
{
count++;
}
public static void main(String[] args)
{
NumOfObjects no1=new NumOfObjects();
System.out.println("no1:" + count);//1
NumOfObjects no2=new NumOfObjects();
System.out.println("no2:"+ count); //2
for (int i=0; i<10;i++)
{
NumOfObjects noi=new NumOfObjects();
}
System.out.println("Total objects:"+count);// 12
}
}
class Test1
{
static int count=0;
public Test1()
{
count++;
System.out.println("Total Objects"+" "+count);
}
}
public class CountTotalNumberOfObjects
{
public static void main(String[] args)
{
Test1 t = new Test1();
Test1 t1 = new Test1();
Test1 t3 = new Test1();
Test1 t11 = new Test1();
Test1 t111 = new Test1();
Test1 t13 = new Test1();
}
}

- 2,994
- 9
- 29
- 43
public class ObjectCount
{
static int i;
ObjectCount()
{
System.out.println(++i);
}
public static void main(String args[])
{
ObjectCount oc = new ObjectCount();
ObjectCount od = new ObjectCount();
ObjectCount oe = new ObjectCount();
ObjectCount of = new ObjectCount();
ObjectCount og = new ObjectCount();
}
}

- 6,356
- 3
- 60
- 69

- 9
- 1