There are several references in Java: Strong references, softreference,weakreference, and Phantomrefrence. Can be simply called: strong, soft, weak, virtual. The strength of the reference is decremented in turn.
A strong reference means that the object is still being used and cannot be recycled.
A soft reference can be recycled, but the time to recycle is when the reference object no longer has a strong reference to it, and the JVM is out of memory.
A weak reference can be recycled, it does not affect the GC, and the object is recycled according to the normal recycling mechanism. You can continue to use the object when it is not recycled. Therefore, the use of soft references requires a null check.
A virtual reference can be recycled, and it does not affect the GC. A virtual reference to an object is not available, that is, the virtual reference object cannot be reused. Virtual references are primarily used to prepare the time when the object is being GC.
Combining the characteristics of various references, you can find that softreference is more suitable for caching because it can reside in memory, but does not cause oom. The timing of weakreference is not intended to affect the normal collection of objects, such as container storage. Phantomrefrence is seldom used. One use of phantomrefrence is to solve the misuse of the Java destructor Finilze (overwriting the Finilze and creating a new strong reference), because phantomrefrence is recycled after the Finilze is executed.
Both Softreference,weakreference and Phantomrefrence can accept a referencequeue<t> parameter. When the objects they refer to are recycled, the referenced objects are placed in the referencequeue<t>.
Looking at an example, there is a better understanding of the three references.
Public class reftest { public static void main (String args[]) { string s1 = new string ("abc") ); string s2 = new string ("Def"); string s3 = new string ("Hij"); ReferenceQueue<String> srq = new ReferenceQueue< String> (); softreference<string> ss = New softreference<string> (S1,&NBSP;SRQ); Referencequeue<string> wrq = new referencequeue<string> (); weakreference<string> ws = new weakreference<string > (S2,&NBSP;WRQ); referencequeue<string> prq = new referencequeue< String> (); phantomreference<string> ps = new PhantomReference<String> (S3,&NBSP;PRQ); System.out.println ("ps = " + ps.get ()); s1 = null; s2 = null; &NBSP;&NBSP;&NBSP;&NBSP;S3&NBSP;=&NBSP;NULL;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SYSTEM.GC (); system.out.println ("ss = " + ss.get ()); system.out.println ("srq = " + srq.poll ()); system.out.println ("ws = " + ws.get ()); system.out.println ("srq = " + wrq.poll ()); system.out.println ("ps = " + ps.get ()); system.out.println ("prq = " + prq.poll ()); }}
16 rows using a virtual reference to get the object is not feasible. The resulting object is null.
18-20 lines frees a strong reference on a three-character object.
22 rows try to do a GC, releasing S2 and S3. S1 also has soft references, and memory is also sufficient, so it will not be recycled.
24-25 rows try to use a soft reference, which is still valid, so the corresponding Referencequeue is empty.
26-27 rows try to use a weak reference, because the strong reference to S2 has been released, the weak reference has been invalidated, and the corresponding Referencequeue is placing a weak reference that has been invalidated.
28-29 rows attempt to use a virtual reference, because the strong reference to S3 has been freed, the weak reference has been invalidated (even if the strong reference is still valid), and the corresponding Referencequeue is placing a virtual reference that has been invalidated.
The final output:
PS = NULLSS = ABCSRQ = NULLWS = NULLWRQ = [email protected]ps = NULLPRQ = [email protected]
Several references to Java