Strong references, soft references, and weak references in Java

Source: Internet
Author: User

Strong references, soft references, and weak references in Java
Strong references in Java, soft references to SoftReference, weak references to WeakReference, and virtual references
There are four types of references in Java:

1. Strong reference
We usually use the most reference. 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. GC may start to recycle memory only when the allocated memory object no longer has any reference.

1 A obj = new A (); 2... 3 obj = null; now obj can be recycled.

2. Soft reference SoftReference:
SoftReference is often used for high-speed cache. It is characterized by GC only when the memory is insufficient, instead of setting it to null (the object is inaccessible, to recycle it! 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. 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. Weak reference WeakReference
It is actually an "indirect" reference. When we want to obtain information about an object at any time, but do not want to affect the garbage collection of this object, you should use Weak Reference to remember this object.
The difference between weak references and soft references is that 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.
1 A obj = new A (); 2 WeakReference wr = new WeakReference (obj); 3 obj = null; 4 5 // wait for A while, obj object will be reclaimed by garbage collection 6... 7 8 if (wr. get () = null) {9 System. out. println ("obj already cleared"); 10} else {11 System. out. println ("obj has not been cleared, and its information is" obj. toString (); 12}


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.