Java four reference types (strong reference, soft reference, weak reference, phantom reference) detailed explanation

Let me talk about the conclusion first

Strength from strong to weak: strong reference > soft reference > weak reference > phantom reference

In Java, a reference refers to a pointer or handle to an object used in a program, which can be used to access the properties and methods of the object. There are four different types of references in Java: strong references, soft references, weak references, and phantom references.

1. Strong Reference

Strong references are the most common reference type and are also the default reference type. If an object has strong references, the garbage collector will not reclaim the object even in low memory conditions . For example:

Object obj = new Object();

In this example, obj is a strong reference pointing to a new Object object. As long as obj exists, the object will not be reclaimed by the garbage collector.

2. Soft Reference

A soft reference is a type of reference that is weaker than a strong reference. If an object has only soft references, then when the system runs out of memory, the garbage collector may reclaim the object, but not necessarily . Soft references are usually used to cache data, and if the system is low on memory, some data in the cache can be cleaned up. For example:

SoftReference<Object> softRef = new SoftReference<>(new Object());

In this example, softRef is a soft reference that points to a new Object object. If the system runs out of memory, the garbage collector can reclaim the object, but not necessarily.

3. Weak Reference

Weak references are weaker than soft references. If an object has only weak references, the object will be reclaimed when the garbage collector runs, regardless of whether there is sufficient memory . Weak references are often used to implement callback functions and listeners because they avoid memory leaks. For example:

WeakReference<Object> weakRef = new WeakReference<>(new Object());

In this example, weakRef is a weak reference that points to a new Object object. When the garbage collector runs, the object will be reclaimed regardless of whether there is enough memory.

4. Phantom Reference

Phantom references are the weakest type of reference. If an object has only phantom references, it may be recovered by the garbage collector at any time . Phantom references are usually used to manage off-heap memory. For example, in DirectByteBuffer, phantom references can be used to track the garbage collector to release off-heap memory when there is insufficient memory. For example:

ReferenceQueue<Object> queue = new ReferenceQueue<>();
PhantomReference<Object> phantomRef = new PhantomReference<>(new Object(), queue);

In this example, phantomRef is a phantom reference that points to a new Object object and has an associated ReferenceQueue. When the garbage collector reclaims the object, it places a reference in the

Guess you like

Origin blog.csdn.net/qq_45796667/article/details/129915623