Four kinds of reference types for Java objects __java

Source: Internet
Author: User
Tags garbage collection

Reference Blog: http://my.oschina.net/Bruce370/blog/511707

In previous versions of JDK 1.2, if an object was not referenced by any variable, the program could no longer use the object. In other words, only the object is in a reachable state and the program can use it. Starting with JDK version 1.2, the reference to the object is divided into 4 levels, giving the program more flexibility to control the life cycle of the object. These 4 levels, from highest to lowest, are: Strong references, soft references, weak references, and virtual references.

⑴ Strong reference (Strongreference)
Strong references are the most common references used. If an object has a strong reference, the garbage collector will never reclaim it. When memory space is low, the Java virtual machine would rather throw a outofmemoryerror error, cause the program to terminate abnormally, and not recycle the strongly referenced object to solve the out-of-memory problem. PS: Strong reference is actually the meaning of our usual a A = new A ().

⑵ Soft Reference (SoftReference)
If an object has only a soft reference, the memory space is sufficient, the garbage collector will not recycle it, and if the memory space is low, the memory of those objects is reclaimed. The object can be used by the program as long as the garbage collector does not recycle it. Soft references can be used to implement memory-sensitive caching (shown below for example).
A soft reference can be used in conjunction with a reference queue (Referencequeue), and the Java Virtual machine will add the soft reference to the reference queue associated with it if the object referenced by the soft reference is reclaimed by the garbage collector.

⑶ Weak reference (WeakReference)
A weak reference differs from a soft reference in that an object that has only a weak reference has a more ephemeral lifecycle. When the garbage collector thread scans the area of memory it governs, it reclaims its memory whenever it discovers an object that has only a weak reference, regardless of whether the current memory space is sufficient or not. However, because the garbage collector is a very low priority thread, it is not necessarily easy to find those objects that have only weak references.
A weak reference can be used in conjunction with a reference queue (Referencequeue), and the Java virtual machine adds the weak reference to the reference queue associated with it if the object referenced by the weak reference is garbage collected.

⑷ Virtual Reference (phantomreference)
"Virtual Reference" as the name suggests, is a fake, and several other references are different, the virtual reference does not determine the life cycle of the object. If an object holds only a virtual reference, it is likely to be reclaimed by the garbage collector at any time, as without any reference.
Virtual references are used primarily to track activities that objects are reclaimed by the garbage collector. One difference between a virtual reference and a soft reference and a weak reference is that a virtual reference must be used in conjunction with a reference queue (Referencequeue). When the garbage collector is ready to reclaim an object, if it finds a virtual reference, it adds the virtual reference to the associated reference queue before reclaiming the object's memory.

Referencequeue queue = new Referencequeue ();

Phantomreference PR = new Phantomreference (object, queue);
A program can determine whether a referenced object will be garbage collected by determining whether a virtual reference has been added to the reference queue. If the program finds that a virtual reference has been added to the reference queue, you can take the necessary action before the memory of the referenced object is reclaimed.

(a) Why soft references are used
The SoftReference feature is that an instance of it holds soft references to a Java object that does not prevent garbage collection threads from reclaiming the Java object. That is, once SoftReference has saved a soft reference to a Java object, the Get () method provided by the SoftReference class returns a strong reference to the Java object before the garbage thread reclaims the Java object. Also, once the garbage thread reclaims the Java object, the Get () method returns NULL. Soft reference objects are easy to manipulate and will not force the presence of memory, which is an excellent way of caching.
(ii) Use of soft references
Example: Caching a soft reference bitmap object with a map collection

map<string, softreference<bitmap>> imagecache = new New hashmap<string, softreference<bitmap> > ();
Strongly referenced Bitmap object
Bitmap Bitmap = Bitmapfactory.decodestream (InputStream);
Soft reference Bitmap Object
softreference<bitmap> bitmapcache = new softreference<bitmap> (Bitmap);
Add the object to the map so that it caches
imagecache.put ("1", softrbitmap);
The Bitmap object that takes soft references from the cache
softreference<bitmap> Bitmapcache = Imagecache.get ("1");
Remove the Bitmap object, and if the Bitmap is recycled due to insufficient memory, the null
Bitmap BM = Bitmapcache. get ();

Using soft references to cache pictures into memory, is an Android image caching common method, it is relatively flexible, will not seize memory, easy to recycle.

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.