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

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

问题描述

我已阅读 这篇文章介绍了Java中不同类型的引用(强、软、弱、幻影),但我不是很懂.

I have read this article about different types of references in Java (strong, soft, weak, phantom), but I don't really understand it.

这些引用类型之间有什么区别,每种类型将在什么时候使用?

What is the difference between these reference types, and when would each type be used?

推荐答案

Java 提供了两种不同类型/类的引用对象:strongweak强>.弱引用对象可以进一步分为softphantom.

Java provides two different types/classes of Reference Objects: strong and weak. Weak Reference Objects can be further divided into soft and phantom.

  • 强大
    • 幻影

    让我们一点一点.

    强参考对象

    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.

    弱点

    可以招募两种不同程度的弱点:softphantom.

    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 no memory is available and there is risk of OutOfMemoryError (in that case, it can be removed).

    另一方面,幻影引用对象仅用于准确知道对象何时从内存中有效删除:通常它们用于修复奇怪的 finalize() revival/复活行为,因为它们实际上并不返回对象本身,而只是帮助跟踪他们的记忆存在.

    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天全站免登陆