强引用,软引用,弱引用,虚引用的使用

本文详细介绍了Java中的四种引用类型:强引用、软引用、弱引用和虚引用的特点及应用场景,并给出了具体的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Java中没有指针的概念,而引用就是一个弱化的指针,保证开发不能任意操作内存。最近整理了一下之前不明白的各种级别引用:强引用、软引用、弱引用、虚引用,它们的特点和应用场景汇总如下:

1、强引用

    如果一个对象具有强引用,GC绝不会回收它;当内存空间不足,JVM宁愿抛出OutOfMemoryError错误。一般new出来的对象都是强引用,如下

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. //强引用  
  2. User strangeReference=new User();  
      

2、软引用

     如果一个对象具有软引用,当内存空间不足,GC会回收这些对象的内存,使用软引用构建敏感数据的缓存。

     在JVM中,软引用是如下定义的,可以通过一个时间戳来回收,下面引自JVM:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public class SoftReference<T> extends Reference<T> {  
  2.   
  3.     /** 
  4.      * Timestamp clock, updated by the garbage collector 
  5.      */  
  6.     static private long clock;  
  7.   
  8.     /** 
  9.      * Timestamp updated by each invocation of the get method.  The VM may use 
  10.      * this field when selecting soft references to be cleared, but it is not 
  11.      * required to do so. 
  12.      */  
  13.     private long timestamp;  
  14.   
  15.     /** 
  16.      * Creates a new soft reference that refers to the given object.  The new 
  17.      * reference is not registered with any queue. 
  18.      * 
  19.      * @param referent object the new soft reference will refer to 
  20.      */  
  21.     public SoftReference(T referent) {  
  22.         super(referent);  
  23.         this.timestamp = clock;  
  24.     }  
  25.   
  26.     /** 
  27.      * Creates a new soft reference that refers to the given object and is 
  28.      * registered with the given queue. 
  29.      * 
  30.      * @param referent object the new soft reference will refer to 
  31.      * @param q the queue with which the reference is to be registered, 
  32.      *          or <tt>null</tt> if registration is not required 
  33.      * 
  34.      */  
  35.     public SoftReference(T referent, ReferenceQueue<? super T> q) {  
  36.         super(referent, q);  
  37.         this.timestamp = clock;  
  38.     }  
  39.   
  40.     /** 
  41.      * Returns this reference object's referent.  If this reference object has 
  42.      * been cleared, either by the program or by the garbage collector, then 
  43.      * this method returns <code>null</code>. 
  44.      * 
  45.      * @return   The object to which this reference refers, or 
  46.      *           <code>null</code> if this reference object has been cleared 
  47.      */  
  48.     public T get() {  
  49.         T o = super.get();  
  50.         if (o != null && this.timestamp != clock)  
  51.             this.timestamp = clock;  
  52.         return o;  
  53.     }  
  54.   
  55. }  
    软引用的声明的借助强引用或者匿名对象,使用泛型SoftReference<T>;可以通过get方法获得强引用。具体如下:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. //软引用  
  2. SoftReference<User>softReference=new SoftReference<User>(new User());  
  3. strangeReference=softReference.get();//通过get方法获得强引用  


3、弱引用  

     如果一个对象具有弱引用,在GC线程扫描内存区域的过程中,不管当前内存空间足够与否,都会回收内存,使用弱引用 构建非敏感数据的缓存。

     在JVM中,弱引用是如下定义的,下面引自JVM:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public class WeakReference<T> extends Reference<T> {  
  2.   
  3.     /** 
  4.      * Creates a new weak reference that refers to the given object.  The new 
  5.      * reference is not registered with any queue. 
  6.      * 
  7.      * @param referent object the new weak reference will refer to 
  8.      */  
  9.     public WeakReference(T referent) {  
  10.         super(referent);  
  11.     }  
  12.   
  13.     /** 
  14.      * Creates a new weak reference that refers to the given object and is 
  15.      * registered with the given queue. 
  16.      * 
  17.      * @param referent object the new weak reference will refer to 
  18.      * @param q the queue with which the reference is to be registered, 
  19.      *          or <tt>null</tt> if registration is not required 
  20.      */  
  21.     public WeakReference(T referent, ReferenceQueue<? super T> q) {  
  22.         super(referent, q);  
  23.     }  
  24.   
  25. }  

    弱引用的声明的借助强引用或者匿名对象,使用泛型WeakReference<T>,具体如下:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. //弱引用  
  2. WeakReference<User>weakReference=new WeakReference<User>(new User());  

4、虚引用

     如果一个对象仅持有虚引用,在任何时候都可能被垃圾回收,虚引用与软引用和弱引用的一个区别在于:虚引用必须和引用队列联合使用,虚引用主要用来跟踪对象 被垃圾回收的活动。

     在JVM中,虚引用是如下定义的,下面引自JVM:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public class PhantomReference<T> extends Reference<T> {  
  2.   
  3.     /** 
  4.      * Returns this reference object's referent.  Because the referent of a 
  5.      * phantom reference is always inaccessible, this method always returns 
  6.      * <code>null</code>. 
  7.      * 
  8.      * @return  <code>null</code> 
  9.      */  
  10.     public T get() {  
  11.         return null;  
  12.     }  
  13.   
  14.     /** 
  15.      * Creates a new phantom reference that refers to the given object and 
  16.      * is registered with the given queue. 
  17.      * 
  18.      * <p> It is possible to create a phantom reference with a <tt>null</tt> 
  19.      * queue, but such a reference is completely useless: Its <tt>get</tt> 
  20.      * method will always return null and, since it does not have a queue, it 
  21.      * will never be enqueued. 
  22.      * 
  23.      * @param referent the object the new phantom reference will refer to 
  24.      * @param q the queue with which the reference is to be registered, 
  25.      *          or <tt>null</tt> if registration is not required 
  26.      */  
  27.     public PhantomReference(T referent, ReferenceQueue<? super T> q) {  
  28.         super(referent, q);  
  29.     }  
  30.   
  31. }  
      虚引用PhantomReference<T>的声明的借助强引用或者匿名对象,结合泛型ReferenceQueue<T>初始化,具体如下:
[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. //虚引用  
  2. PhantomReference<User> phantomReference=new PhantomReference<User>(new User(),new ReferenceQueue<User>());  

5.应用实例


待添加


asdasd


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值