Strong reference, soft reference, weak reference, phantom reference

What are the four types of references in Java and their application scenarios?

  • Strong reference : Usually the reference returned when we use the new operator to create an object is a strong reference
  • Soft reference : If an object can only be reached through a soft reference, the object will be recycled when the memory is insufficient and can be used in the image cache. When the memory is insufficient, the system will automatically recycle the Bitmap that is no longer used.
  • Weak reference : If an object can only be reached by weak reference, it will be recycled (even if the memory is sufficient), and it can also be used in the image cache. At this time, as long as the Bitmap is no longer used, it will be recycled
  • Virtual reference : The virtual reference is the "weakest" reference in Java. You can't even get the referenced object through it. The only function of its existence is that when the object it points to is recycled, it will be added to the reference queue, so that We can know when the object it points to is destroyed.

Focus on weak references

Weak reference in Java refers specifically to the java.lang.ref.WeakReference class. Let's first take a look at the description of it in the official documentation:

The existence of a weakly referenced object does not prevent the object it points to from being collected by the garbage collector. The most common use of weak references is to implement canonicalizing mappings (such as hash tables).
Suppose the garbage collector decides at some point that an object is weakly reachable (that is to say, all current references to it are weak references), then the garbage collector will clear all weak references to the object, Then mark the weakly reachable object as finalizable so that it will be recycled later. At the same time or later, the garbage collector will put those weak references that have just been cleared into the reference queue (Reference Queue) specified when the weak reference object was created.

In fact, there are four kinds of references in Java, which are from strong to weak: strong reference, soft reference, weak reference, and virtual reference. Let's briefly introduce the other three kinds of references except weak references:
- Strong Reference: Usually, the reference returned when we create a new object through new is a strong reference. If an object passes through a series of strong references, it can be Reach, it is strongly reachable (strongly reachable), then it will not be recycled
- Soft Reference (Soft Reference): The difference between a soft reference and a weak reference is that if an object is reachable by a weak reference, no matter whether the current memory is sufficient or not It will be recycled, and objects reachable by soft references will be recycled when there is insufficient memory, so soft references are "stronger" than weak references
- Phantom Reference: Phantom Reference is the weakest reference in Java , then how weak is it? It is so fragile that we can't even get the referenced object through the virtual reference. The only function of the virtual reference is that when the object it points to is recycled, the virtual reference itself will be added to the reference queue to record it. The pointed-to object has been reclaimed.

Why use weak references?

Consider the following scenario: Now there is a Product class representing a product, this class is designed to be non-extensible, and at this time we want to add a number to each product. One solution is to use HashMap

How to use weak references?

Taking the scenario introduced above as an example, we use a weak reference object pointing to the Product object as the key of the HashMap, just define the weak reference object like this:

Product productA = new Product(...);
WeakReference<Product> weakProductA = new WeakReference<>(productA);

Now, if the reference object weakProductA points to the Product object productA. So how do we get the Product object productA it points to through weakProduct? It's very simple, just need the following code:

Product product = weakProductA.get();

In fact, for this case, the Java class library provides us with the WeakHashMap class. Using this class, its key is naturally a weak reference object, so we don't need to wrap the original object manually. In this way, when productA becomes null (indicating that the Product it refers to no longer needs to exist in memory), then the weak reference object weakProductA points to the Product object, so obviously the corresponding Product object is weak at this time. Reachable, so the weak reference pointing to it will be cleared, the Product object will be recycled immediately, and the weak reference object pointing to it will enter the reference queue.

reference queue

Let's briefly introduce the concept of reference queue. Actually, the WeakReference class has two constructors:

//创建一个指向给定对象的弱引用
WeakReference(T referent) 
//创建一个指向给定对象并且登记到给定引用队列的弱引用
WeakReference(T referent, ReferenceQueue<? super T> q) 

We can see that a parameter of type ReferenceQueue is provided in the second constructor. By providing this parameter, we register the created weak reference object on a reference queue, so that when it is cleared by the garbage collector, it will be By sending it into this reference queue, we can manage these cleared weak reference objects uniformly.

refer to

Understanding Weak References in Java in 10 Minutes

Understanding Weak References in Java

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324607341&siteId=291194637