Java Four types of references

Source: Internet
Author: User

As of today's version, Java references are divided into four types: strong, soft, weak, and virtual (also known as Phantom References), which introduce these four references in JDK1.2, which enables the development of virtual scopes to control the life cycle of Java objects to a certain extent. The virtual scope is because, even with non-strong references, the final garbage collection initiation is determined by the JVM garbage collection mechanism, but this change is an improvement that makes us more flexible in our applications.
Strong references:
Generally used in Java development is a strong reference, it is called a strong reference, if the reference is not shown empty or out of scope, that is, the object associated with such a reference is not recycled, even if the JVM memory is tight, rather than throw a outofmemory exception, will not reclaim such reference associated objects.
Example:

ObjectnewObject();Objectnull;

This is a strong reference, and the reference holds the handle to the object when the reference is emptied or out of scope, and the GC cannot reclaim such a reference.
Soft references (SoftReference):
Soft references are used to describe objects that are useful but not necessary. For objects associated with soft references, the objects associated with the application are recycled when the JVM is running out of memory. This kind of reference perfectly solves the cached reference, when the memory is sufficient, the effect is the same, when the memory is low, survival is the same, releasing the soft reference associated object to meet the JVM's memory requirements.
Example:

ObjectnewObject();SoftReference<Objectnew SoftReference<Objectnull;

Even if the obj=null is set, the objects associated with the soft reference SR are not recycled, and the objects held by the soft reference are not recycled until the JVM memory is tight. This feature of soft references is widely used in the application cache, whether it is back-end data or front-end page caching.
Weak references (WeakReference):

ObjectnewObject();WeakReference<Objectnew WeakReference<Objectnull;

After the obj=null is set, the GC that follows is recommended to reclaim the associated object with a weak reference, but it is not necessarily the GC after setting the Obj=null.
Virtual references (phantomreference):(quote a word from another article)
"Virtual Reference", as the name implies, is a dummy, unlike several other references, a virtual reference does not determine the object's life cycle. If an object holds only virtual references, it can be garbage collected at any time, just as there are no references.
Virtual references are primarily used to track the activities of objects that are garbage collected. One difference between a virtual reference and a soft reference and a weak reference is that the virtual reference must be used in conjunction with the reference queue (Referencequeue). When the garbage collector prepares to reclaim an object, if it finds that it has a virtual reference, it will add the virtual reference to the reference queue associated with it before reclaiming the object's memory. The program can see if the referenced object is going to be garbage collected by judging whether the reference queue has been added to the virtual reference. If the program finds that a virtual reference has been added to the reference queue, it can take the necessary action before the memory of the referenced object is recycled.
Why introduce references in Java, in the sense that strong references, soft references, and weak references are often used in development except for the fourth kind of virtual references, but for soft references and weak references, it is important to note that because of the special processing characteristics of the JVM, it is useful to prevent the use of an object that is still in use when it is recycled by GC.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java Four types of references

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.