When the package is first introduced in the Java 2 platform, java.lang.ref which contains SoftReference , WeakReference and PhantomReference Three reference classes, the primary function of the reference class is to be able to reference objects that can still be reclaimed by the garbage collector. We can only use strong references (strong reference) before we introduce a reference class. For example, the following line of code shows a strong reference obj :
Object obj = new Object (); |
objThis reference will refer to an object stored in the heap. As long as the obj reference exists, the garbage collector will never release the storage space used to hold the object.
When obj out of scope or explicitly specified null , the garbage collector considers that there are no other references to this object and can collect it. However, you also need to pay attention to one important detail: only objects can be collected, which does not mean that the garbage collector will be able to reclaim it once it is specified to run. Due to the different garbage collection algorithms, some algorithms analyze shorter-lived objects more frequently than older, longer-lived objects. Therefore, an object that is available for collection may never be recycled. This can happen if the program ends before the garbage collector releases the object. So, in a nutshell, you never guarantee that the objects that are available for collection will always be collected by the garbage collector.
This information is important for you to analyze reference classes. Because garbage collection has a specific nature, reference classes may not actually be as useful as you might have imagined, although they are useful classes for specific problems. Soft references (soft reference), weak references (weak reference), and virtual references (phantom Reference) objects provide three different ways to reference a heap object without blocking the collection. Each reference object behaves differently, and the interaction between them and the garbage collector differs. In addition, these new reference classes exhibit a "weaker" reference form than a typical strong reference. Also, an object in memory can be referenced by multiple references, which can be strong references, soft references, weak references, or virtual references. Before we go further, let's look at some of the terms:
- strongly reachable object (strongly reachable): an object that can be accessed by a strong reference.
- soft Reachable object (softly reachable): An object that is not a strong and accessible object, and that can be accessed through a soft reference.
- weakly reachable objects (weakly reachable): objects that are not strongly reachable and are not soft-reachable objects, and can be accessed through weak references.
- Virtual objects (phantomly reachable): objects that are not strongly reachable, soft, and objects, and are not weakly reachable objects, that have ended, that can be accessed through a virtual reference.
- clear: Sets the referent field of the reference object to
null , and declares the object referenced in the heap as being terminated .
SoftReference class
SoftReferenceA typical use of a class is for memory-sensitive caches. SoftReferenceis to ensure that all soft references are cleared before the JVM reports out-of-memory conditions while maintaining a reference to the object. The key is that the garbage collector may (and may not) release soft-and-accessible objects at run time. Whether an object is disposed depends on the algorithm of the garbage collector and the amount of memory available when the garbage collector is running.
WeakReference class
WeakReferenceA typical use of a class is normalized mapping (canonicalized mapping). In addition, weak references are useful for objects that have a relatively long lifetime and are not expensive to recreate. The key is that when the garbage collector runs, it will release the referenced object if it encounters a weak and accessible object WeakReference . Note, however, that the garbage collector may have to run multiple times to find and release a weak and accessible object.
Phantomreference class
PhantomReferenceClass can only be used to track the upcoming collection of referenced objects. Again, it can be used to perform pre-mortem cleanup operations. PhantomReferencemust be ReferenceQueue used with the class. Required ReferenceQueue because it can act as a notification mechanism. When the garbage collector determines that an object is a virtual object, the PhantomReference object is placed ReferenceQueue on it. PhantomReferenceputting the object ReferenceQueue on top is also a notification that PhantomReference the object referenced by the object has ended and is ready for collection. This allows you to take action just before the memory that the object occupies is recycled.
The reference class strong reference in Java. The difference between SoftReference, WeakReference and Phantomreference