Reference is an abstract class, and Softreference,weakreference,phantomreference and finalreference are the concrete classes that inherit it.
Next, we will introduce and analyze the characteristics and usages of the strong references and the various virtual references under the Java.lang.ref package respectively.
Characteristics and usage of strongreference, SoftReference, WeakReference and Phantomreference
Strongreference:
We all know that the objects in the JVM are allocated on the heap, and when the program does not have references to the object, the object can be reclaimed by the garbage collector. The reference referred to here is the variable (such as String, object, ArrayList, etc.) of the object type declared in our general sense, and the variables (such as int, short, long, etc.) that are distinguished from the original data type are also referred to as strong references.
Before we understand virtual references, we typically use strong references to refer to objects. Such as:
1. Strongreference usage
New String ("T");
The tag reference here is called a strong reference. Strong references have the following characteristics:
A strong reference can directly access the target object. The object that the strong reference points to is not reclaimed by the system at any time. A strong reference can cause a memory leak.
The three kinds of Reference that we're going to talk about are "weak references" compared to strong references, which means that the objects they reference are recycled by the JVM's garbage collector as long as they don't have a strong reference, and the timing and usage of their collection are different. The following are discussed separately.
SoftReference:
SoftReference is the strongest reference in "weak references". SoftReference points to an object that, when no strong reference points to it, stays in memory for a period of time, and the garbage collector decides whether to recycle it based on the usage of the JVM's memory (the degree of memory shortage) and the call of the SoftReference get () method. (later chapters will be elaborated in several experiments)
The specific use is generally through the construction of SoftReference, will need to use weak reference to point to the object wrapped up. When needed, call SoftReference's Get () method to get it. The Get () method of SoftReference returns a strong reference to the object when it is not reclaimed. As follows:
2. SoftReference usage
New Softreference<bean> (new Bean ("name")); System. out. println (Bean. Get()); // "Name:10"
Soft references have the following characteristics:
Get the () method gets a strong reference to the object to access the target object. The object that the soft reference points to is based on the usage of the JVM (whether the Heap memory is near the threshold) to determine whether to recycle. Soft references can avoid exceptions caused by insufficient Heap memory.
When the garbage collector decides to reclaim it, it empties its softreference, which means that the softreference get () method will return null and then call the object's Finalize () method, which is actually recycled in the next round of GC.
WeakReference:
WeakReference is a reference type that is weaker than softreference. Weak references are similar in nature to soft references, except that the object pointed to by a weak reference is garbage collected by the system and is always recycled regardless of memory usage (The Get () method returns null)
It is possible to operate the weakreference in the same way as SoftReference, which is not repeated here.
Weak references have the following characteristics:
Get the () method gets a strong reference to the object to access the target object. once the system memory is reclaimed, the object that the weak reference points to is recycled, regardless of the memory tension. weak references can also avoid exceptions caused by insufficient Heap memory.
Phantomreference:
Phantomreference is the weakest reference type in all "weak references". Unlike soft and weak references, virtual references cannot use the Get () method to obtain a strong reference to the target object, thereby using the target object, and observing that the source can find that get () is overridden to return null forever.
What is the purpose of the virtual reference? In fact, a virtual reference is primarily used to track the state of the object being garbage collected, and to take action by looking at whether the reference queue contains a virtual reference to the object and whether it is about to be garbage collected. It is not expected to be used to obtain a reference to the target object, and its reference will be put into a Referencequeue object before the target object is recycled, thus achieving the purpose of tracking object garbage collection.
So the exact usage differs from the previous two, it must pass in a Referencequeue object. A virtual reference is added to this queue when the object referenced by the virtual reference is garbage collected. Such as:
3. Phantomreference Usage
Public Static voidMain (string[] args) {Referencequeue<String> Refqueue =NewReferencequeue<string>(); Phantomreference<String> referent =NewPhantomreference<string>( NewString ("T"), refqueue); System. out. println (referent.Get());//NULLSystem.GC (); System.runfinalization (); System. out. println (Refqueue.poll () = = referent);//true}
It is important to note that for reference recycling, a virtual reference similar to a strong reference does not automatically reclaim the target object based on the memory situation, and the Client needs to handle it itself to prevent the Heap out of memory exception.
A virtual reference has the following characteristics:
A virtual reference can never use the Get () method to obtain a strong reference to an object to access the target object.
The virtual reference itself is placed in the Referencequeue object to track the object garbage collection until the object it points to is reclaimed by system memory.
Virtual references do not automatically reclaim target objects based on memory conditions.
It is also worth noting that, in fact, SoftReference, WeakReference, and Phantomreference constructors can receive a Referencequeue object. When SoftReference and WeakReference are emptied, that is, when the Java garbage collector prepares to reclaim the objects they point to, they are added to this referencequeue before invoking the object's Finalize () method. Object, you can now fetch them through the Referencequeue poll () method. Phantomreference is added to the Referencequeue object only when the Java garbage collector actually recycles the object it points to, so that it can chase the destruction of the object.
Let's review the performance of the four reference types and the performance of the garbage collector when it recycles the cleanup memory.
Soft Reference (SoftReference), the reference type behaves as if the memory is nearing full load, or if the call to the object by the Softreference.get () method does not occur for a period of time, the garbage collector cleans up the object. The soft Reference object is added to the Referencequeue before the object's Finalize method is run.
Weak reference (WeakReference), a reference type that behaves as if the system garbage collector starts to recycle, the reference to that object is reclaimed immediately. As with soft references, weak references also add weak reference objects to Referencequeue before the Finalize method of the object is run.
Strong references (finalreference), which are the most commonly used reference types. The JVM system uses Finalizer to manage each strongly referenced object and to add referencequeue when it is marked for cleanup, and invokes the Finalize () method of the object one at a time.
Virtual reference (Phantomreference), which is one of the most unreal reference types. The object referenced by the virtual reference cannot be returned again, no matter where. Virtual references when the system garbage collector starts to reclaim objects, the Finalize () method is called directly, but it is not immediately added to the recycle queue. Only when the real object is cleared by the GC will it be added to the Reference queue.
When the system garbage collection is run multiple times, the IBM JVM adds the soft reference to the Recycle queue and runs its Finalize method. In addition, virtual references are not added to the queue even after a lot of system garbage collection. I don't know if this is a small BUG in the IBM JVM.
The performance of the Oracle JVM in SoftReference meets specifications and is only recycled when memory is low. The IBM JVM's strategy is even more aggressive, and it's worth noting that the memory is still plentiful.
The performance of the Oracle JVM in phantomreference satisfies the specification, and several GC implementations are added to the Queue after finalize. The IBM JVM has never been added to the Queue causing a dead loop. So when you use Phantomreference, you can consider whether the use of different JVMs is the result of a similar situation.
Java Reference WeakReference