Java Learning | What is the difference between a strong reference, a soft reference, a weak reference, a fantasy reference?

Source: Internet
Author: User

In the Java language, in addition to the basic data types, the others are object references to various types of objects, and in Java the references are divided into 4 classes based on the length of their life cycles.

1 Strong references

Features: Our usual typical code object obj = obj in new object () is a strong reference. The reference that is associated with the object created by the keyword new is a strong reference. When the JVM is running out of memory space, the JVM prefers to throw a outofmemoryerror run-time error (OOM), which causes the program to terminate abnormally, and does not rely on random recycling of strongly referenced "surviving" objects to resolve out-of-memory issues. For an ordinary object, if there is no other reference relationship, as long as the scope of the reference is exceeded or the corresponding (strong) reference is explicitly assigned to NULL, it can be garbage collected, the specific time to recycle is to look at the garbage collection strategy.

2 Soft References

Features: Soft references are implemented through the SoftReference class. The life cycle of a soft reference is shorter than a strong reference. The JVM attempts to reclaim the object pointed to by a soft reference only if it considers it to be low on memory: that is, the JVM ensures that the object pointed to by the soft reference is cleaned before the OutOfMemoryError is thrown. 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. Later, we can call Referencequeue's poll () method to check if there are objects that it cares about being recycled. If the queue is empty, a null is returned, otherwise the method returns a reference object in front of the queue.

Scenario: Soft references are often used to implement memory-sensitive caches. If you have free memory, you can temporarily reserve the cache and clean it up when there is not enough memory, which ensures that the cache is used without running out of memory.

3 Weak references

Weak references are implemented through the WeakReference class. The life cycle of a weak reference is shorter than a soft reference. As the garbage collector thread scans the area of memory it governs, once an object with a weak reference is found, its memory is reclaimed regardless of whether the current memory space is sufficient or not. Because the garbage collector is a low-priority thread, it does not necessarily recover weakly referenced objects very quickly. 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.

Application scenario: Weak applications can also be used for memory-sensitive caches.

4 Virtual References

Features: Virtual reference is also called Phantom Reference, through the Phantomreference class to achieve. You cannot access any property or function of an object through a virtual reference. Phantom references simply provide a mechanism to make certain things happen after the object has been finalize. 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 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 a program discovers that a virtual reference has been added to the reference queue, it can take some procedural action before the memory of the referenced object is recycled.

Scenario: An activity that can be used to track an object being reclaimed by the garbage collector and receive a system notification before a virtual reference is associated with an object that is reclaimed by the garbage collection

Java Learning | What is the difference between a strong reference, a soft reference, a weak reference, a fantasy reference?

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.