This is the 30th day of my participation in the August Text Challenge.More challenges in August

WangScaler: A writer with heart.

Declaration: uneducated, if there is a mistake, kindly correct.

Java has four major references that let developers manage the life cycle of objects. Strong reference, soft reference, weak reference, soft reference. Let’s take a brief look today.

  • Strong references: When memory is low, it is not collected, so strong references are one of the causes of memory leaks. When strong references are not in use, we should weaken them so that GC can collect them.

  • Soft reference: when there is enough memory, it is not reclaimed, but when there is no memory, it is reclaimed.

  • Weak references: When the garbage collector finds it, it is collected.

  • Vreference: must be used in conjunction with the reference queue. When vreference objects are collected by GC, they are added to the reference queue, and the program can do other things depending on whether the reference queue has added the object.

Strong reference

Objects that are new to us are strong references.

Object wangscaler=new Object();
Copy the code

Because strong references are not recycled, if our object is no longer in use, yes

wangscaler = null;
Copy the code

Weaken it and wait for the garbage to be recycled.

Soft references

Soft references are usually used for caching. When there is enough memory, some data is cached and when there is not enough memory, it is reclaimed. Techniques such as image processing add images uploaded by users to the cache.

SoftReference<Object> wangscaler = new SoftReference<>(new Object());
Copy the code

A weak reference

WeakReference<Object> wangscaler = new WeakReference();
Copy the code

Weak references are similar to soft references and can also be used for caching, except that weak references have a shorter lifetime and are recycled every time GC occurs, so they should be used for less frequent applications.

Phantom reference

ReferenceQueue<Object> wangscaler = new ReferenceQueue<>();
PhantomReference<Object> scaler = new PhantomReference<>(new Object(), wangscaler);
Copy the code

Must be used with reference queues. We can use virtual references if we have objects that are being recycled for special treatment. It acts as a notification mechanism, like Spring’s post-notification, to tell us that the object has been reclaimed. The underlying ThreadLocal code uses virtual references. Note that the result of a virtual reference to get() is always null, and the object cannot be retrieved whether or not it has been cleaned.

Reference queue

Soft, weak and virtual references can be used together with the ReferenceQueue of the ReferenceQueue. When the three referenced objects are collected, they are added to the reference queue, which provides insight into JVM garbage collection.

contrast

Reference types Recovery time
Strong reference Never recycled
Soft references Out of memory
A weak reference When the GC
Phantom reference

conclusion

Use four kinds of references to determine the life cycle of objects. Just like our daily necessities, some items are thrown into the trash can for recycling, some items are thrown out when they are no longer fit at home, and some items can never be thrown away. So when memory runs out, OutOfMemory is thrown and strongly referenced objects are not reclaimed.

A very important use of virtual references is to free out-of-heap memory. DirectByteBuffer uses virtual references to free out-of-heap memory.

Come all come, click “like” and then go!

Follow WangScaler and wish you a promotion, a raise and no bucket!