java types_Java Reference Types

References

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

Strong Reference

StringBuilderbuilder =newStringBuilder();

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.

Weak Reference

WeakReference weakBuilder = new WeakReference(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 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 Reference

Garbage collector can collect an object if only weak references are pointing towards it and they are eagerly collected, on the other hand Objects with SoftReference are collected when JVM absolutely needs memory.

Phantom Reference

Object which only has Phantom reference pointing them can be collected whenever Garbage Collector likes it.

Useful Classes

WeakHashMap

Hash table based implementation of the Map interface, with weak keys. An entry in a WeakHashMap will automatically be removed when its key is no longer in ordinary use. More precisely, the presence of a mapping for a given key will not prevent the key from being discarded by the garbage collector, that is, made finalizable, finalized, and then reclaimed. When a key has been discarded its entry is effectively removed from the map, so this class behaves somewhat differently from other Map implementations.

ReferenceQueue

If the garbage collector discovers an object that is weakly reachable, the following occurs:

The WeakReference object‘s referent field is set to null, thereby making it not refer to the heap object any longer.

The heap object that had been referenced by the WeakReference is declared finalizable.

The WeakReference object is added to its ReferenceQueue. Then the heap object‘s finalize() method is run and its memory freed.

When creating non-strong reference objects we have the option of passing a reference queue as a part of the Reference constructor. As seen from the above explanation, this reference queue is a way for the GC to inform the program that a certain object is no longer reachable.

Example

importjava.lang.ref.ReferenceQueue;importjava.lang.ref.WeakReference;public classTest {public static void main(String[] args) throwsInterruptedException {

SavePoint savePoint= new SavePoint("Random"); //a strong object

ReferenceQueue savepointQ = new ReferenceQueue();//the ReferenceQueue

WeakReference savePointWRefernce = new WeakReference(savePoint, savepointQ);

System.out.println("SavePoint created as a weak ref " +savePointWRefernce);

Runtime.getRuntime().gc();

System.out.println("Any weak references in Q ? " + (savepointQ.poll() != null));

savePoint= null; //the only strong reference has been removed. The heap//object is now only weakly reachable

System.out.println("Now to call gc...");

Runtime.getRuntime().gc();//the object will be cleared here - finalize will be called.

System.out.println("Any weak references in Q ? " + (savepointQ.remove() != null));

System.out.println("Does the weak reference still hold the heap object ? " + (savePointWRefernce.get() != null));

System.out.println("Is the weak reference added to the ReferenceQ ? " +(savePointWRefernce.isEnqueued()));

}

}classSavePoint {publicSavePoint(String value) {

}

}

The program :

Creates a strong reference and adds it to a Weak reference savePointWRefernce. The object in memory is now referenced by a strong reference and a weak reference - hence strongly reachable.

The first call to garbage collector will not clear our savepoint object as it is a strong reference. Hence the poll method of the referenceQ will return null. (poll method is non - blocking it checks and returns immediately.)

The savePoint reference variable is set to null. Our heap object is now referenced only by the weak reference - hence it is weakly reachable.

The second gc call will now locate the object, executes its finalize method and mark this object to be freed. Theobject is also added to the ReferenceQ.

A call to the remove method of the ReferenceQ will return the object. remove is a blocking method. it will wait till an object has been made available in the Queue. (poll method might not work as the recycling process is happening on a separate thread.)

As seen once the object was ready to be released it was added to the reference queue. So the reference queue is like a callback to our java program, telling us that a particular object is released from its reference and is not available to our code anymore.

As seen from above the Reference queue actually holds within it the WeakReference which lost its heap object to clean up. The WeakReference does not have any association to the memory object. The get call above returns null. Unlike with finalize when we can make the object alive again, with the ReferenceQ there is no way to reach the released java object. Reference Queues are just for References that got freed by garbage collection. They cannot be used to make alive our objects again. They can only be used to notify our code about the loss of memory objects referred to by these non- strong references.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值