Is it possible to get the object reference count?

前端 未结 2 705
甜味超标
甜味超标 2020-12-03 06:44

I\'d like to know if there is a way to check how many references a Java object has. As far as I could check the only way to do that is using JVMTI through a JNI interface. I

2条回答
  •  北荒
    北荒 (楼主)
    2020-12-03 07:23

    From your description, it seems you care less about the actual count of references than to simply know when an object has been collected. If this is the case, you can use WeakReference or PhantomReference to determine when a referenced object is ready for finalization.

    See:

    • Javadoc for WeakReference: http://download.oracle.com/javase/6/docs/api/java/lang/ref/WeakReference.html
    • Javadoc for PhantomReference: http://download.oracle.com/javase/6/docs/api/java/lang/ref/PhantomReference.html
    • A Great Explanation of Weak references: https://community.oracle.com/blogs/enicholas/2006/05/04/understanding-weak-references
    • Garbage Collector callbacks: http://java.dzone.com/articles/letting-garbage-collector-do-c
    • Garbage collection notification?

    Hope this helps.

提交回复
热议问题