How to find the number of objects in the heap

前端 未结 10 1790
离开以前
离开以前 2020-11-29 07:31

How can I find the number of live objects on the heap in Java program?

相关标签:
10条回答
  • 2020-11-29 07:57

    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.

    0 讨论(0)
  • 2020-11-29 08:04

    Use jvisualvm, and do a memory sample. It will show the number of classes and instances:

    enter image description here

    0 讨论(0)
  • 2020-11-29 08:04

    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());
    
    0 讨论(0)
  • 2020-11-29 08:07
    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 
        }
    }
    
    0 讨论(0)
  • 2020-11-29 08:12

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

    0 讨论(0)
  • 2020-11-29 08:15

    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.

    0 讨论(0)
提交回复
热议问题