当前位置:网站首页>What is the difference between strong reference, soft reference, weak reference and virtual reference?

What is the difference between strong reference, soft reference, weak reference and virtual reference?

2022-07-24 11:21:00 51CTO

  Strong citation 、 Soft citation 、 Weak reference 、 Virtual reference · Language sparrow (yuque.com)

since JDK1.2 Start ,Java Provides 4 Kinds of reference relationships , To represent the relationship between the reference and the instance object .

Strong citation “ Live forever ”

Strong citation , Is our most common common common common object reference .

As long as strong quotes still exist , The garbage collector will never recycle the referenced instance object .

Object o = new Object();

     
  • 1.

Soft citation “ Yes n A second chance to live ”

Before the system is about to run out of memory , The garbage collector will recycle instance objects that are only associated with soft references , If there is not enough memory for this collection , An out-of-memory exception is thrown (OutOfMemoryError).


Soft references are often used to implement memory sensitive caching :

  • If you have free memory , You can keep the cache temporarily ;
  • If there is not enough memory , Then clean up the cache ;

This ensures that the cache is used at the same time , Does not run out of memory .

SoftReference<Object> softO = new SoftReference<>(new Object());

     
  • 1.

Weak reference “ Recycle and die ”

Instance objects associated with weak references can only survive until the next garbage collection , When the garbage collector is working , Whether the current memory is enough or not , Will recycle the instance objects that are only weakly referenced .


Weak references can be used to build a relationship without specific constraints , such as , Maintain a non mandatory mapping relationship , If the object is still there when you try to get it , Just use it , Otherwise, it will be realistic and exemplary . Weak references are also the choice of many cache implementations .

WeakReference<Object> weakO = new WeakReference(new Object());

     
  • 1.

Virtual reference “ It can be recycled at any time ”

Virtual references are also called “ Ghost quotes ” perhaps “ Phantom reference ”, Virtual reference is the weakest kind of reference relationship .

  • stay Java 8 And in previous versions , After virtual reference recycling , The object pointed to by the virtual reference will be recycled .
  • stay Java 9 And the latest version , Virtual references have no effect on the lifetime of objects .

Cannot get an instance object by virtual reference .

Virtual references are mainly used to track the garbage collection activities of objects , Reclaim resources associated with objects .

stay Java 8 And in previous versions , When the garbage collector is ready to recycle an instance object , If it's found to have virtual references , Before recycling the instance object , Add this virtual reference to the reference queue associated with it . If the program finds that a virtual reference has been added to the reference queue , Then you can take the necessary action before the memory of the referenced object is recycled , Then disconnect the reference of the virtual reference object , Virtual references are recycled or unreachable , The object pointed to by the virtual reference will be recycled .

Object counter = new Object();
ReferenceQueue refQueue = new ReferenceQueue<>();
PhantomReference<Object> p = new PhantomReference<>(counter, refQueue);
counter = null;
System.gc();
try {
    // Remove  It's a blocking method , You can specify  timeout, Or choose to block all the time 
    Reference<Object> ref = refQueue.remove(1000L);
    if (ref != null) {
        // do something
        
        ref = null;
    }
} catch (InterruptedException e) {
    // Handle it
}

     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.

Reference material

  The first 4 speak | Strong citation 、 Soft citation 、 Weak reference 、 What's the difference between phantom quotes ?<br />

 ThreadLocal Principle analysis and memory leak demonstration

原网站

版权声明
本文为[51CTO]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/205/202207241109180432.html