Four types of references in Java

Source: Internet
Author: User

In previous versions of JDK 1.2, if an object was not referenced by any variable, the program could no longer use the object. That is, only the object is in the accessible (reachable) state before the program can use it. Starting with JDK version 1.2, the reference to the object is divided into 4 levels, giving the program more flexibility in controlling the object's life cycle. These 4 levels are high to low in order: Strong references, soft references, weak references, and virtual references.

Java memory management is divided into memory allocation and memory recycling, no programmer is responsible for garbage collection mechanism is mainly to see whether the object has a reference to the object.

These four reference types are available in Java for a total of two purposes:

The first is to allow the programmer to determine the life cycle of certain objects through code;

The second is to help the JVM with garbage collection.

⑴ Strong references (strongreference)
Strong references are the most commonly used references. If an object has a strong reference, the garbage collector will never recycle it.  When there is not enough memory space, the Java virtual Machine prefers to throw a outofmemoryerror error, which causes the program to terminate abnormally, and does not rely on random recycling of strongly referenced objects to resolve out-of-memory issues. PS: Strong references are actually the meaning of our usual a = new A ().

If you want to break the association between a strong reference and an object, you can display the reference assignment to NULL, so that the JVM reclaims the object at the appropriate time.

For example, in the clear method of the vector class, the cleanup is done by assigning a reference to NULL.

⑵ Soft Reference (SoftReference)
an object has only soft references, and if the memory space is sufficient, the garbage collector does not recycle it, and if the memory space is insufficient, the memory of those objects is reclaimed . The object can be used by the program as long as it is not reclaimed by the garbage collector. soft references can be used to implement memory-sensitive caches . such as Web caching, image caching, and so on. Using soft references can prevent memory leaks and enhance the robustness of the program.

  SoftReference is characterized by an instance of it that holds a soft reference to a Java object, The existence of this soft reference does not prevent the garbage collection thread from reclaiming the Java object.

That is, once SoftReference has saved a soft reference to a Java object, the Get () method provided by the SoftReference class returns a strong reference to the Java object before the garbage thread recycles the Java object. Also, once a garbage thread reclaims the Java object, the Get () method returns NULL.

A soft reference can be used in conjunction with a reference queue (Referencequeue), and if the object referenced by the soft reference is reclaimed by the garbage collector, the Java Virtual machine will add the soft reference to the reference queue associated with it.

⑶ Weak references (weakreference)
The difference between a weak reference and a soft reference is that an object with only a weak reference has a shorter life cycle. As the garbage collector thread scans the area of memory it governs, once an object with only a weak reference is found, its memory is reclaimed, regardless of whether the current memory space is sufficient or not. However, because the garbage collector is a low-priority thread, it is not necessarily quick to discover objects that have only weak references.
A weak reference can be used in conjunction with a reference queue (Referencequeue), and if the object referenced by the weak reference is garbage collected, the Java virtual machine adds the weak reference to the reference queue associated with it.

⑷ Virtual Reference (phantomreference)
"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 reclaimed by the garbage collector at any time, just as there are no references.
virtual references are primarily used to track activities that objects are reclaimed by the garbage collector. 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.

Referencequeue queue = new Referencequeue ();

Phantomreference PR = new Phantomreference (object, queue);
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 discovers 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 reclaimed.

Four types of references in Java

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.