Strong references, soft references, weak references in Java

Source: Internet
Author: User

Strong references in Java, soft reference softreference, weak reference weakreference, virtual reference
There are four types of references in Java:

1. Strong references
The most common reference we use. 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. The GC may start to reclaim its memory only if the allocated memory object no longer has any references.
<span style= "FONT-SIZE:18PX;" >1 a obj = new A (); 2 ..... 3 obj =null; At this point obj can be recycled. </span>

2. Soft reference softreference:
SoftReference is often used for cache caches, which is characterized by It's not that you set it to null (the object is unreachable), and the GC recycles it, but only when the memory is low, it's recycled!   If an object has only soft references, enough memory space is available, 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. 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. &NBSP

3. Weak reference WeakReference
is actually an "indirect" reference when we want to get information about an object at any time, but do not want to affect the garbage collection of this object, then you should use the Weak Reference to remember this object. The difference between a
  weak reference and a soft reference is that the weakly referenced object 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.   Weak references 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.
 
<span style= "FONT-SIZE:18PX;" > 1 A obj = new A (); 2     WeakReference wr = new WeakReference (obj), 3     obj = null, 4  5     //wait for a period of time, obj object will be garbage collected 6 ... 7  8 if (Wr.get () ==null) {9 System.out.println ("obj has been cleared"), or else {one System.out.println ("obj has not been cleared, its information is" obj.t Ostring ());}</span>


Strong references, soft references, weak 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.