Strong, soft, weak, and virtual references of Java objects + ReferenceQueue, javareferencequeue

Source: Internet
Author: User

Strong, soft, weak, and virtual references of Java objects + ReferenceQueue, javareferencequeue
Strong, soft, weak, and virtual references of Java objects + ReferenceQueue 1. Strong references (StrongReference)

Strong references are the most common references. If an object has a strong reference, 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 at will to solve the problem of insufficient memory.

Ii. SoftReference)

If an object only has soft references, the memory space is sufficient and 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 (as shown in the following example ).

Soft references can be used together with a ReferenceQueue. If the referenced objects are recycled by the garbage collector, the Java virtual machine adds this soft reference to the reference queue associated with it.

3. WeakReference)

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 by the garbage collector at any time.

Virtual references are used to track the activity of objects recycled by the garbage collector. 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 garbage collector is preparing 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.

  1. ReferenceQueue queue =NewReferenceQueue ();
  2. PhantomReference pr =NewPhantomReference (object, queue );

The program can determine whether the referenced object has been added to the reference queue to check 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.

V. ReferenceQueue

It is a Reference queue. If the Reference object itself is saved, if the object referenced by Reference is recycled by GC, the Reference is no longer valid.

This type of Reference will be placed in the Reference queue. You can clear it here to avoid occupying space.

Vi. WeakHashMap

Weak reference map: The Key is a weak reference Key. If the Key is recycled, the value is automatically removed after the value of the map is obtained.

If the Key is always strongly referenced, it cannot be recycled;

Note that Value is strongly referenced, so do not allow Value to indirectly reference the Key key, which will cause the Key clock to be strongly referenced.

This is suitable for cache controlled by the Key's lifecycle.

 

 

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.