是否可以获取对象引用计数? [英] Is it possible to get the object reference count?

查看:428
本文介绍了是否可以获取对象引用计数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有办法检查Java对象有多少引用。据我所知,唯一的方法就是通过JNI接口使用JVMTI。是否有纯Java(不使用本机库)解决方案来获取此信息?

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. Is there a pure java (without using native libraries) solution to get this information?

我们正在开发一种用于数据结构动画的教育工具(用于学生实施某些算法),如果我们能够以最非侵入性的方式检查已发布的对象,那将是非常好的(我试图避免强迫此工具的用户调用ObjectReleased(objRef)等方法为了删除元素或更新某些东西而更新数据结构动画。

We are developing an educational tool for data structure animation(to be used with students implementation of certain algorithms), so it would be very nice if we could check for "released" objects on the most non-intrusive way (I´m trying to avoid forcing the user of this tool to call a method such as ObjectReleased(objRef) in order to update the data structure animation for an element removal or something simmilar)

推荐答案

从您的描述来看,您似乎不太关心引用的实际数量,而不是简单地知道何时收集了一个对象。如果是这种情况,您可以使用WeakReference或PhantomReference来确定引用的对象何时可以完成。

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.

请参阅:

  • 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?

希望这有帮助。

这篇关于是否可以获取对象引用计数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆