Interviewer: Talk about four ways to reference Java objects in detail

  • 2021-07-26 07:34:55
  • OfStack

Preface

We know that in Java, except for the basic data type, everything else is a reference type.

Java divides reference types into strong reference, soft reference, weak reference and phantom reference according to the length of its life cycle.

Normally, we basically only use strong reference types, while other reference types can only be seen during interviews or when reading class libraries or other framework source codes on weekdays.

1. Strong citation

We use new on weekdays, and one object is a strong reference, for example Object obj = new Object(); When JVM runs out of memory space, you would rather throw OutOfMemoryError to cause the program to terminate abnormally than recycle a living object with a strong reference!

Remember that new1 objects will never be recycled by GC if they are alive. When an ordinary object has no other reference relationship, as long as it exceeds the scope of the reference or the reference is assigned to null, your object indicates that it is not alive, so it can be recycled by GC. Of course, the recovery time is not fixed, depending on the GC recovery strategy.

2. Soft references

The life cycle of soft references is 1% shorter than that of strong references. Soft references are implemented through the SoftReference class.


Object obj = new Object();
SoftReference softObj = new SoftReference(obj);
obj = null ;  // Remove strong references 

This is a simple way to use soft references. Objects are obtained through the get () method. When JVM thinks there is not enough memory space, it goes back and tries to recycle the object pointed to by the soft reference, that is, before JVM throws OutOfMemoryError, it will clean up the soft reference object. Soft references can be used in conjunction with reference queues (ReferenceQueue).


Object obj = new Object();
ReferenceQueue queue = new ReferenceQueue();
SoftReference softObj = new SoftReference(obj,queue);
obj = null ;  // Remove strong references 

When the obj referenced by softObj is recycled by GC, the softObj object will be stuffed into queue, and then we can check whether the object you care about has been recycled through poll () of this queue. If the queue is empty, we will return an null. On the contrary, it returns a soft reference object, that is, softObj.

Soft reference 1 is generally used to realize memory-sensitive cache. If there is free memory, it can be reserved, and when there is insufficient memory, it can be cleaned up, so as to ensure that the cache will not be exhausted while using it. For example, caching pictures in the picture cache framework is through soft reference.

3. Weak references

A weak reference is implemented through the WeakReference class, which has a shorter life cycle than a soft reference, and obtains objects through the get () method.


 Object obj = new Object();
 WeakReference<Object> weakObj = new WeakReference<Object>(obj);
 obj = null ;  // Remove strong references 

In GC, this object will be recycled regardless of insufficient memory space, which can also be used with ReferenceQueue and is also suitable for memory-sensitive caching. key in ThreadLocal uses weak references.

4. Phantom citations

Also known as virtual reference, it is implemented through PhantomReference class. It may be recycled by GC at any time, just like not referencing 1.


Object obj = new Object();
ReferenceQueue queue = new ReferenceQueue();
PhantomReference<Object> phantomObj = new PhantomReference<Object>(obj , queue);
obj = null ;  // Remove strong references 

You cannot access any properties or functions of an object through virtual references. Then we have to ask. What's the use of it? Virtual references simply provide a mechanism to ensure that objects are later done by finalize. For example, after this object is recycled, a system notification will be sent. Virtual references must be used in conjunction with ReferenceQueue. The specific usage method is the same as that of soft references mentioned above. Mainly used to track the activity of objects being garbage collected.

Summarize


Related articles: