Strong references, soft references, weak references, and virtual references in Java

Source: Internet
Author: User
In Java strong reference, soft reference, weak reference and Virtual Reference original link: http://aaronfu.net /? P = 9995

Since jdk1.2, the object reference is divided into four levels, so that the program can control the object lifecycle more flexibly. The four levels are from high to low: strong reference, soft reference, weak reference, and virtual reference.

1. Strong reference
The references mentioned above in this chapter are actually strong references, which are the most common references. 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.

2. Soft reference)

If an object only has soft references, it is similar to the necessities that can be used. If the memory space is sufficient, the garbage collector will not recycle it. If the memory space is insufficient, the memory of these objects will be recycled. The object can be used by programs as long as the garbage collector does not recycle it. Soft references can be used to implement memory-sensitive high-speed cache.
Soft references can be used together with a referencequeue, the Java virtual machine adds this soft reference to the reference queue associated with it.

3. weakreference)
If an object only has weak references, it is similar to a necessities that can be used. The difference between weak references and soft references is that only objects with weak references have a shorter life cycle. When the Garbage Collector thread scans the memory area under its jurisdiction, its memory will be recycled no matter whether the current memory space is sufficient or not. However, since the garbage collector is a thread with a low priority, it may not soon find objects with weak references.
Weak references can be used together with a referencequeue, the Java virtual machine adds this weak reference to the reference queue associated with it.

4. phantomreference)
As the name implies, "virtual references" are just the same as virtual ones, which are different from other types of references. Virtual references do not determine the object lifecycle. If an object only holds a virtual reference, it is the same as no reference and may be recycled at any time.
Virtual references are mainly used to track the activity of objects being reclaimed by garbage collection. A difference between virtual and soft references and weak references is that virtual references must be used together with the reference Queue (referencequeue. When the zookeeper recycler is about to recycle an object, if it finds that it has a virtual reference, it will add this virtual reference to the reference queue associated with it before it recycles the object's memory. The program can know whether the referenced queue is added with virtual references.

Whether the referenced object is to be recycled. If the program finds that a virtual reference has been added to the reference queue, it can take necessary action before the memory of the referenced object is recycled.

In this book, "Reference" can be either a verb or a noun. Readers should distinguish the meaning of "Reference" based on context.

The Java. Lang. Ref package provides three classes: softreference class, weakreference class, And phantomreference class, which represent soft reference, weak reference, and virtual reference respectively. The referencequeue class indicates the reference queue. It can be used with these three reference classes to track the collection of referenced objects by the Java Virtual Machine. The following program creates a String object, referencequeue object, and weakreference object:

// Create a strong reference
String STR = new string ("hello ");

// Create a reference queue. <string> indicates the reference of the string object in the queue.
Referencequeue <string> RQ = new referencequeue <string> ();

// Create a weak reference that references the "hello" object and is associated with the RQ reference queue
// <String> it is a fan mark, indicating that weakreference will weakly reference the string object
Weakreference <string> WF = new weakreference <string> (STR, rq );

After the above code is executed, the relationship between the reference and the object in the memory is 11-10.

Figure 11-10 "hello" objects have both strong references and weak references

In Figure 11-10, arrows with solid lines represent strong references, and arrows with dotted lines represent weak references. It can be seen that the "hello" object is strongly referenced by STR and weak referenced by a weakreference object. Therefore, the "hello" object will not be recycled.
In the following program code, set the STR variable that references the "hello" object to null, and then obtain the reference of the "hello" object through the weakreference weak reference get () method:

String STR = new string ("hello"); // ①
Referencequeue <string> RQ = new referencequeue <string> (); // ②
Weakreference <string> WF = new weakreference <string> (STR, rq); // ③

STR = NULL; // ④ cancel the strong reference of the "hello" Object
String str1 = WF. Get (); // ⑤ if the "hello" object is not recycled, str1 references the "hello" Object

// If the "hello" object is not recycled, RQ. Poll () returns NULL.
Reference <? Extends string> ref = RQ. Poll (); // 6

After executing the fourth row above, the relationship between the reference and the object in the memory is shown in 11-11. At this time, the "hello" object only has weak references, so it may be garbage collection. If it has not been reclaimed by garbage collection, the WF. Get () method executed in row ⑤ will return to the reference of the "hello" object and make the object highly referenced by str1. Then, when the RQ. Poll () method is executed in Row 6, null is returned because no reference is provided in the reference queue. The poll () method of referencequeue is used to return the reference in the queue. If not, null is returned.


Figure 11-11 "hello" objects only have weak references

In the following program code, after executing line 4, the "hello" object only has weak references. The next two calls the system. GC () method to urge the Garbage Collector to work, so as to increase the possibility that the "hello" object will be recycled. If the "hello" object is recycled, the reference of the weakreference object is added to the referencequeue, and then WF. the get () method returns NULL, and RQ. the poll () method returns the reference of the weakreference object. Figure 11-12 shows the relationship between the reference in the memory and the object after the execution of the nth row.

String STR = new string ("hello"); // ①
Referencequeue <string> RQ = new referencequeue <string> (); // ②
Weakreference <string> WF = new weakreference <string> (STR, rq); // ③
STR = NULL; // ④

// Push the Garbage Collector twice to improve the possibility of "hello" objects being recycled
System. GC (); // ⑤
System. GC (); // ⑥
String str1 = WF. Get (); // 7 if the "hello" object is recycled, str1 is null
Reference <? Extends string> ref = RQ. Poll (); // returns


Figure 11-12 The "hello" object is garbage collected and weak references are added to the reference queue.

The important part about strong references-the part that makes them "strong"-is how they interact with the garbage collector. specifically, if an object is reachable via a chain of strong references (strongly reachable), it is not eligible for garbage collection. as you don't want the Garbage Collector destroying objects you're working on, this is normally exactly what you want.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.