Java:强/弱/弱/幻像参考之间的区别 [英] Java: difference between strong/soft/weak/phantom reference

查看:145
本文介绍了Java:强/弱/弱/幻像参考之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已阅读这篇文章这个话题,但我真的不明白。
在描述概念时,请给我一些建议和示例。

I have read this article about the topic, but I don't really understand it. Please give me some advice along with examples when describing the concepts.

推荐答案

Java提供两种不同的类型/类参考对象。弱引用对象可以进一步分为 soft phantom 。让我们一点一点地去。

Java provides two different types/classes of Reference Objects: strong and weak. Weak Reference Objects can be further divided into soft and phantom. Let's go point by point.

强参考对象

StringBuilder builder = new StringBuilder();

这是参考对象的默认类型/类,如果没有不同的指定: builder 是一个强大的参考对象。这种引用使引用的对象不符合GC的条件。也就是说,每当一个对象被强引用对象链引用时,它就不能被垃圾回收。

This is the default type/class of Reference Object, if not differently specified: builder is a strong Reference Object. This kind of reference makes the referenced object not eligible for GC. That is, whenever an object is referenced by a chain of strong Reference Objects, it cannot be garbage collected.

弱引用对象

WeakReference<StringBuilder> weakBuilder = new WeakReference<StringBuilder>(builder);

弱参考对象不是参考对象的默认类型/类,要使用它们应该是明确的如上例所示。这种引用使参考对象符合GC的条件。也就是说,如果内存中 StringBuilder 对象唯一可达的引用实际上是弱引用,则GC允许垃圾收集 StringBuilder 对象。当内存中的对象只能通过弱引用对象访问时,它将自动符合GC的条件。

Weak Reference Objects are not the default type/class of Reference Object and to be used they should be explicitly specified like in the above example. This kind of reference makes the reference object eligible for GC. That is, in case the only reference reachable for the StringBuilder object in memory is, actually, the weak reference, then the GC is allowed to garbage collect the StringBuilder object. When an object in memory is reachable only by Weak Reference Objects, it becomes automatically eligible for GC.

弱点级别

可以使用两种不同级别的弱点: soft phantom

Two different levels of weakness can be enlisted: soft and phantom.

soft 参考对象基本上是一个弱的参考对象,它在内存中保留了一点:通常,它会阻止GC循环直到内存可用并且没有 OutOfMemoryError 的风险(在这种情况下,可以将其删除)。

A soft Reference Object is basically a weak Reference Object that remains in memory a bit more: normally, it resists GC cycle until memory is available and there is no risk of OutOfMemoryError (in that case, it can be removed).

另一方面,幻像参考对象仅用于准确了解对象何时从内存中有效删除:通常它们是用于修复怪异的终结()复活/复活行为,因为它们实际上并不返回对象本身,而只是帮助跟踪他们的记忆存在

On the other hand, a phantom Reference Object is useful only to know exactly when an object has been effectively removed from memory: normally they are used to fix weird finalize() revival/resurrection behavior, since they actually do not return the object itself but only help in keeping track of their memory presence.

弱参考对象是理想的实现缓存模块。事实上,只要强引用链无法再访问对象/值,GC就可以实现一种自动驱逐。一个例子是 WeakHashMap 保留弱密钥。

Weak Reference Objects are ideal to implement cache modules. In fact, a sort of automatic eviction can be implemented by allowing the GC to clean up memory areas whenever objects/values are no longer reachable by strong references chain. An example is the WeakHashMap retaining weak keys.

这篇关于Java:强/弱/弱/幻像参考之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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