Four types of references in Java

Source: Internet
Author: User

We know that the Java language provides 4 types of references: Strong references, soft references (softreference), weak references (WeakReference), and Ghost References (Phantomreference), which are closely related to references, There is also a reference queue referencequeue. The relationship between reference and reference queues is important for garbage collection, and learning the garbage collection mechanism must first understand the use of reference and reference queues.

1. Strong references

Not be GC, when space is low. The JVM prefers to throw a outofmemory error, which causes the program to terminate, and does not randomly reclaim objects with strong references to resolve out-of-memory issues. Examples of strong references: Object Obj=new object ();

2. Soft reference

With enough memory space, the garbage collector does not recycle it, and if the memory space is insufficient, the memory of those objects is reclaimed. A soft reference is a memory-sensitive cache. A soft reference can be used in conjunction with a reference queue, and if the object referenced by the soft reference is garbage collected, the Java virtual Opportunity adds the soft application to the reference queue associated with it .

3. Weak references

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. Very soft references are similar, just recycled faster. Weak reference objects are often used in map data structures to refer to objects that occupy large memory space .

4. Ghost Reference Virtual Reference when the garbage collector is ready to reclaim an object, if when it finds a virtual reference, it adds 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 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. Because of the unsafe and inefficient nature of the object.finalize () method, it is common to use virtual references to complete the resource release work before the object is reclaimed. Refer to my other blog: explaining why finalize is unsafe and not recommended

When a GC discovers a virtual reference object, the Phantomreference object is inserted into the Referencequeue queue. * At this point the object pointed to by Phantomreference is not recycled by GC, but is not recycled until referencequeue is processed by you.

In particular, it is important to note that when the JVM inserts a virtual reference into the reference queue, the object memory that the virtual reference executes is still present. But Phantomreference does not expose the API to return objects. So if I want to do cleanup work, I need to inherit the Phantomreference class so that I can access the object it points to. such as the automatic recovery of NiO Direct memory, it is used to the Sun.misc.Cleaner

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.