Several references in Java
Java has several different reference methods: strong reference, soft reference, weak reference, and virtual reference. Next, we will first have a detailed understanding of the significance of these reference methods.
Strong reference
The references we introduced earlier are strongly referenced, which is the most common reference. If an object has a strong reference, it is similar to an essential necessities. The Garbage Collector will never recycle it. When the memory space is insufficient, the Java Virtual Machine would rather throw an outofmemoryerror to terminate the program abnormally and does not recycle strongly referenced objects to solve the problem of insufficient memory.
Softreference)
Softreference is typically used for memory-sensitive high-speed caching. Softreference is used to ensure that all soft references are cleared before the JVM reports insufficient memory when the object is referenced. The key is that the Garbage Collector may (or may not) release software and objects at runtime. Whether the object is released depends on the garbage collector algorithm and the amount of memory available when the garbage collector is running.
Weakreference)
A typical use of the weakreference class is canonicalized mapping ). In addition, for objects with relatively long lifetime and Low Re-creation overhead, weak references are also useful. The key is that if the spam collector encounters a weak object during runtime, The weakreference reference object will be released. However, note that the spam Collector may have to run multiple times to locate and release weak accessible objects.
Phantomreference)
The phantomreference class can only be used to track the forthcoming collection of referenced objects. It can also be used for pre-mortem cleanup. Phantomreference must be used with the referencequeue class. Referencequeue is required 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 on its referencequeue. Placing the phantomreference object in the referencequeue is also a notification, indicating that the object referenced by the phantomreference object has ended and can be collected. This allows you to take action just before the memory occupied by the object is recycled. Reference and referencequeue.
Interaction between GC, reference, and referencequeue
A. GC cannot delete the memory of strongly referenced objects.
B. GC finds a soft-referenced object memory, so:
① The referent field of the softreference object is set to null, so that the object does not reference the heap object.
② The heap object referenced by softreference is declared as finalizable.
③ When the finalize () method of the heap object is run and the memory occupied by the object is released, the softreference object is added to its referencequeue (if the latter exists ).
C. If GC finds that there is only a weak referenced object memory, then:
① The referent field of the weakreference object is set to null, so that the object does not reference the heap object.
② The heap object referenced by weakreference is declared as finalizable.
③ When the finalize () method of the heap object is run and the memory occupied by the object is released, the weakreference object is added to its referencequeue (if the latter exists ).
D. If GC finds a virtual referenced object memory, then:
① The heap object referenced by phantomreference is declared as finalizable.
② Phantomreference is added to its referencequeue before the heap object is released.
Note the following points:
1. GC generally does not find soft-referenced memory objects. It only discovers and releases the memory of soft-referenced objects when the memory is obviously insufficient.
2. GC does not immediately discover and release weak references. Sometimes it is necessary to repeat GC several times before discovering and releasing the memory objects with weak references.
3. When soft reference and weak reference are added to referencequeue, the reference pointing to the real memory has been set to null, and the related memory has been released. However, when the virtual resource is added to the referencequeue, the memory has not been released and can still be accessed.
Address: http://www.blogjava.net/zh-weir/archive/2011/02/23/345007.html