你有没有在任何项目中使用过PhantomReference? [英] Have you ever used PhantomReference in any project?

查看:669
本文介绍了你有没有在任何项目中使用过PhantomReference?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我唯一知道的关于 PhantomReference 是,


  • 如果您使用它的 get() 方法,它将始终返回 null 而不是对象。有什么用??

  • 通过使用 PhantomReference ,可以确保无法从 <$中复活对象c $ c> finalize 方法。

  • If you use its get() method, it will always return null and not the object. What's the use of it?
  • By using PhantomReference, you make it sure that the object cannot be resurrected from finalize method.

但是是这个概念/类的使用?

But what is the use of this concept/class?

你有没有在你的任何项目中使用过这个,或者你有任何我们应该使用它的例子吗?

Have you ever used this in any of your project or do you have any example where we should use this?


推荐答案

我用 PhantomReference s在简单,非常专业的内存分析器监视对象的创建和销毁。我需要他们来追踪破坏。但这种方法已经过时了。 (它是在2004年针对J2SE 1.4编写的。)专业的分析工具功能更强大,更可靠,JMX或代理和JVMTI等新的Java 5功能也可用于此。

I used PhantomReferences in a simplistic, very specialized kind of memory profiler to monitor object creation and destruction. I needed them to keep track of destruction. But the approach is out-dated. (It was written in 2004 targeting J2SE 1.4.) Professional profiling tools are much more powerful and reliable and the newer Java 5 features like JMX or agents and JVMTI can be used for that too.

PhantomReference s(总是与Reference队列一起使用)优于 finalize ,这有一些问题,因此应该避免。主要是使对象再次可达。使用终结器监护人习语可以避免这种情况( - >在有效Java中阅读更多内容)。所以他们也是 new finalize

PhantomReferences (always used together with the Reference queue) are superior to finalize which has some problems and should therefore be avoided. Mainly making objects reachable again. This could be avoided with the finalizer guardian idiom (-> read more in 'Effective Java'). So they are also the new finalize.

此外, PhantomReference s


允许您确定从内存中删除对象的确切时间。他们
实际上是确定这一点的唯一方法。这通常不是
有用,但在某些非常具体的情况下可能会派上用场。
喜欢操纵大图像:如果你确定图像应该是
垃圾收集,你可以等到它实际上是在尝试
加载下一个图像之前,因此可能使可怕的OutOfMemoryError减少
。 (引自 enicholas 。)

并且首先写了 psd ,Roedy Green有一个参考文献的好摘要

And as psd wrote first, Roedy Green has a good summary of references.

这篇关于你有没有在任何项目中使用过PhantomReference?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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