首页 > 编程语言 > 详细

Reference types in Java

时间:2019-07-18 17:06:21      阅读:82      评论:0      收藏:0      [点我收藏+]

There are four types reference types in Java 

Namely, 

StrongReference:

Strong reference is very common in our daily code, for example 

  Object obj = new Object();

  Employee  emp = new Employe (EmployeeName, EmployeeAge);

  As long as Strong Reference exists, the garbage collector will not recycle the object reference,

  And when the memory is no enough, JVM will throw OutOfMemory Error. 

To break the Strong Reference tie: 

  1. Assign the null value for example emp = null;

  2. The object is created within a method 

    在一个方法内部创建的对象,由于强引用保存在栈上,所引用的对象保存在堆空间中,当这个方法运行完成就会退出方法栈,从而让强引用断裂

SoftReference:

  If the memory is still enough, the Garbage collector will not re-cycle it, but if the JVM runs out of Memory, it will be recycled by GC. 

技术分享图片

the out put would be 

Employee [id = 1441, name = KK]

Employee [id = 1441, name = KK]

 

WeakReference:

 技术分享图片

Employee [id = 1441, name = KK]

Null

 no matter if the memory is enough or not , the Reference will be recycled by Garbage collector. 

 

PhantomReference: 

 

技术分享图片

 

To Conclude: 

 

技术分享图片

 

Reference types in Java

原文:https://www.cnblogs.com/codingyangmao/p/11208441.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!