How to find the number of objects in the heap

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

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

10条回答
  •  青春惊慌失措
    2020-11-29 08:19

    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();
    
        }
    
    }
    

提交回复
热议问题