34) Four kinds of reference

Introduction

As we all know that Garbage Collector reclaims memory from objects which are eligible for garbage collection. And this eligibility is decided based upon which kind of references are pointing to that object.

there are four kind of reference in Java :
1. Strong reference
2. Weak Reference
3. Soft Reference
4. Phantom 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.

Strong reference

in the code, String s = “abc” , reference variable s has strong reference to String object “abc”. Any object which has Strong reference attached to it is not eligible for garbage collection.

Weak reference

Weak Reference are represented using java.lang.ref.WeakReference class and you can create Weak Reference by using following code :

Counter counter = new Counter(); // strong reference created
WeakReference<Counter> weakCounter = new WeakReference<Counter>(counter); //weak reference 
counter = null; // now Counter object is eligible for garbage collection

now Counter object is eligible for garbage collection.

one weak reference example

One convenient example of WeakReference is WeakHashMap, which wraps keys as WeakReference which doesn’t prevent them from being Garbage collected.

Soft reference

Soft reference in Java is represented using java.lang.ref.SoftReference class. You can use following code to create a SoftReference in Java

Counter prime = new Counter(); // prime holds a strong reference 
SoftReference<Counter> soft = new SoftReference<Counter>(prime) ; //soft reference variable has SoftReference to Counter Object
prime = null; 

now Counter object is eligible for garbage collection but only be collected when JVM absolutely needs memory, so it can delay collection compared to weak reference.

comparison between weak and soft reference

SoftReference are more suitable for caches and WeakReference are more suitable for storing meta data.

SoftReference looks perfect for implementing caches. When JVM needs memory it removes object which have only SoftReference pointing towards them.

WeakReference is great for storing meta data e.g. storing ClassLoader reference. If no class is loaded then no point in keeping reference of ClassLoader, a WeakReference makes ClassLoader eligible for Garbage collection as soon as last strong reference removed.

Phantom reference

Phantom reference is represented by java.lang.ref.PhantomReference class.Object which only has Phantom reference pointing them can be collected whenever Garbage Collector likes it.

DigitalCounter digit = new DigitalCounter(); // digit reference variable has strong reference  
PhantomReference<DigitalCounter> phantom = new PhantomReference<DigitalCounter>(digit); // phantom reference to object
digit = null;

ReferenceQueue

ReferenceQueue refQueue = new ReferenceQueue(); //reference will be stored in this queue for cleanup

DigitalCounter digit = new DigitalCounter();
PhantomReference<DigitalCounter> phantom = new PhantomReference<DigitalCounter>(digit, refQueue);

Reference of instance will be appended to ReferenceQueue and you can use it to perform any clean-up by polling ReferenceQueue.

Object life-cycle diagram

https://2.bp.blogspot.com/-dponJrixU9Y/UzGXiXveSJI/AAAAAAAABVo/Lc3-d8ZsI2g/s1600/Weak+Strong+Soft+and+Phantom+Reference+in+Java.gif

Read more: http://javarevisited.blogspot.com/2014/03/difference-between-weakreference-vs-softreference-phantom-strong-reference-java.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值