Java - Difference between strong/weak/soft/phantom reference
2018-03-02 19:42
921 查看
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.Strong Reference Object
StringBuilder builder = new StringBuilder();This is the default type/class of Reference Object, if not differently specified:
builderis 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.Weak Reference Object
WeakReference<StringBuilder> weakBuilder = new WeakReference<StringBuilder>(builder);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
StringBuilderobject in memory is, actually, the weak reference, then the GC is allowed to garbage collect the
StringBuilderobject. When an object in memory is reachable only by Weak Reference Objects, it becomes automatically eligible for GC.Levels of WeaknessTwo different levels of weakness can be enlisted: soft and phantom.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.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.
相关文章推荐
- Difference between WeakReference vs SoftReference vs PhantomReference vs Strong reference in Java
- java的4种reference: strong, soft, weak, phantom
- java持有引用(Strong/soft/weak/phantom Reference)
- What is the difference between a soft reference and a weak reference in Java?
- java基础学习(8)4种引用类型简析StrongReference、 SoftReference、 WeakReference 、PhantomReference
- Java的引用StrongReference、 SoftReference、 WeakReference 、PhantomReference
- Java的引用StrongReference、 SoftReference、 WeakReference 、PhantomReference
- Java:WeakReference vs SoftReference vs PhantomReference vs Strong reference
- Java的引用StrongReference、 SoftReference、 WeakReference 、PhantomReference
- java的4种reference: strong, soft, weak, phantom
- Java中的四种引用类型 Strong, Soft, Weak And Phantom
- Java Reference(SoftReference、WeakReference、PhantomReference)的使用
- Java引用对象SoftReference WeakReference PhantomReference
- Java中三个引用类SoftReference 、 WeakReference 和 PhantomReference的区别
- Java引用总结--StrongReference、SoftReference、WeakReference、PhantomReference
- strong/soft/weak/phantom reference比较
- WeakReference vs SoftReference vs PhantomReference vs StrongReference
- Java中三个引用类SoftReference 、 WeakReference 和 PhantomReference的区别
- Java中三个引用类SoftReference 、 WeakReference 和 PhantomReference的区别
- Java引用总结--StrongReference、SoftReference、WeakReference、PhantomReference