Four reference types of Java objects

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

In versions prior to JDK 1.2, if an object is not referenced by any variable, then the program can no longer use this object. That is, a program can only use an object if it is in a reachable state. Starting from version 1.2 of JDK, object references are divided into four levels, so that programs can more flexibly control the life cycle of objects. These 4 levels from high to low are: strong reference, soft reference, weak reference and phantom reference.

(1) Strong reference (StrongReference)
Strong reference is the most commonly used reference. If an object has strong references, the garbage collector will never collect it. When the memory space is insufficient, the Java virtual machine would rather throw an OutOfMemoryError error, causing the program to terminate abnormally, and would not solve the problem of insufficient memory by arbitrarily recycling objects with strong references. ps: Strong reference is actually what we usually mean by A a = new A().

⑵Soft Reference (SoftReference)
If an object has only soft references, the memory space is sufficient, and the garbage collector will not reclaim it; if the memory space is insufficient, the memory of these objects will be reclaimed. As long as the garbage collector does not collect it, the object can be used by the program. Soft references can be used to implement memory-sensitive caches (examples are given below).
Soft references can be used in conjunction with a reference queue (ReferenceQueue). 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.

(3) Weak Reference (WeakReference)
The difference between a weak reference and a soft reference is that objects with only weak references have a shorter life cycle. In the process of scanning the memory area under its jurisdiction by the garbage collector thread, once an object with only weak references is found, its memory will be reclaimed regardless of whether the current memory space is sufficient or not. However, since the garbage collector is a very low-priority thread, objects that only have weak references may not be found quickly.
A weak reference can be used in conjunction with a reference queue (ReferenceQueue). If the object referenced by the weak reference is garbage collected, the Java virtual machine will add the weak reference to the reference queue associated with it.

⑷Phantom Reference (PhantomReference)
"Phantom Reference", as the name suggests, is in name only, unlike other types of references, Phantom Reference does not determine the life cycle of the object. If an object holds only phantom references, it is as if it has no references and may be reclaimed by the garbage collector at any time.
Phantom references are mainly used to track the activity of objects being reclaimed by the garbage collector. One difference between phantom references and soft references and weak references is that phantom references must be used in conjunction with reference queues (ReferenceQueue). When the garbage collector is about to reclaim an object, if it finds that it still has a virtual reference, it will add this virtual reference to the reference queue associated with it before recycling the object's memory.

ReferenceQueue queue = new ReferenceQueue ();

PhantomReference pr = new PhantomReference (object, queue);
The program can know whether the referenced object will be garbage collected by judging whether a phantom reference has been added to the reference queue. If the program finds that a phantom reference has been added to the reference queue, it can take necessary actions before the memory of the referenced object is reclaimed.

(1) Why use soft references
The characteristic of SoftReference is that one of its instances holds a soft reference to a Java object, and the existence of this soft reference does not prevent the garbage collection thread from recycling the Java object. That is to say, once SoftReference saves a soft reference to a Java object, before the garbage thread recycles the Java object, the get() method provided by the SoftReference class returns a strong reference to the Java object. Also, once the Java object is reclaimed by the garbage thread, the get() method will return null. Objects with soft references are easy to manipulate and will not be forced to reside in memory. They are an excellent way to cache.
(2) Use soft references
Example: Use the Map collection to cache soft referenced Bitmap objects

Map<String, SoftReference<Bitmap>> imageCache = new new HashMap<String, SoftReference<Bitmap>>();
//强引用的Bitmap对象
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
//软引用的Bitmap对象
SoftReference<Bitmap> bitmapcache = new SoftReference<Bitmap>(bitmap);
//添加该对象到Map中使其缓存
imageCache.put("1",softRbitmap);
..
.
//从缓存中取软引用的Bitmap对象
SoftReference<Bitmap> bitmapCache = imageCache.get("1");
//取出Bitmap对象,如果由于内存不足Bitmap被回收,将取得空
Bitmap bm = bitmapCache .get();

Using soft references to cache images in memory is a common method for Android image caching. It is relatively flexible, does not occupy memory, and is easy to recycle.

Guess you like

Origin blog.csdn.net/gs12software/article/details/51051813