Java has four types of references: strong application, soft reference, weak reference, and virtual reference

The only difference between the four references is how the GC treats them during garbage collection. Before explaining the four types of references, let’s look at the reference queue.

Reference queue: When an object is collected by gc, references to that object are added to the reference queue. But why save references to objects that have already been reclaimed? Usually after an object is collected by GC, you need to do some cleanup. For example, if you keep a reference to an object in a WeakHashMap, when the object is collected, you need to remove the reference from a WeakHashMap to prevent the map from growing endlessly.

1. Strongly reference StrongReference

In general, objects that we simply new are strongly referenced, such as StringBuffer buffer = new StringBuffer(); We know that in the Jvm objects are accessed by reference, and the same object can have multiple references to it. Strongly applied objects cannot be collected by GC as long as there is currently a strong application pointing to them.

2. SoftReference SoftReference

Soft references are relatively “soft” references for strong applications. As long as there is enough memory, objects referred to by soft references will not be reclaimed by gc, but if memory is not rented, they will be reclaimed.

3, WeakReference

Weak references are “weak” references compared to soft applications, and when the GC scans the JVM for garbage and finds that an object is soft-referenced, the object is reclaimed regardless of whether there is sufficient memory.

WeakReference<Activity> activity = new WeakReference<>(widget);
activity.get();
Copy the code

The referenced object can be retrieved using the get() method, but because of the nature of weak references, we often encounter null returns from activity.get(), so

4. Virtual reference PhantomReference

A virtual reference does not have any “reference” meaning; in other words, an object holding a virtual reference does not hold any reference, and it can be reclaimed by the GC at any time. At the moment, virtual references seem to have no effect, but they exist for a reason, and they are designed to keep track of how gc collects objects. A virtual reference differs from a soft or weak reference in that it must be used in conjunction with a reference queue. When the garbage collector is about to reclaim an object and finds that it has a virtual reference, it adds the virtual reference to the reference queue associated with it before reclaiming the object’s memory.

ReferenceQueue queue = new ReferenceQueue ();
PhantomReference pr = new PhantomReference (object, queue); 
Copy the code

conclusion

According to the above analysis and description, it can be seen that:

  1. In order of strength, strong reference, soft application, weak reference, virtual reference
  2. Soft references are often used to design cache
  3. Virtual references must be used in conjunction with reference queues