How to find the number of objects in the heap

前端 未结 10 1824
离开以前
离开以前 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: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());
    

提交回复
热议问题