How to find the number of objects in the heap

前端 未结 10 1835
离开以前
离开以前 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: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...

提交回复
热议问题