Java records of strong reference, soft reference, weak reference and virtual reference (Practical)

Posted by rdawson on Sun, 14 Jun 2020 09:58:30 +0200

First of all, don't be afraid of some nouns. In fact, they are all reasonable things.

The reference itself is well understood. The reference type data stores the memory address of the actual object. When garbage collection, it depends on whether the object has a reference. Java does not need developers to allocate memory and free memory, but it can handle the life cycle of related objects through four reference types and cooperate with jvm for garbage collection.

1. Strong reference

Normal creation objects and assignment variables are of strong reference type. The strong reference type will not be recycled during garbage collection. When the memory is insufficient, an OutOfMemoryError error will be thrown directly.

byte[] data = new byte[2*1024*1024];
 VM options:-Xms1m -Xmx1m -XX:+PrintGC

For example, in the above example, the jvm specifies a maximum heap memory of 1m. If a program wants to create something of 2m, it will throw an OOM error directly when the program is running. When the reference no longer needs the associated object, null assignment can be performed to facilitate jvm garbage collection.

2. Soft reference

Objects with soft references will not be recycled when there is enough memory, and will not be recycled until OOM occurs. In Java, soft reference is used to declare a soft reference, and get method is used to return the strong reference of the object. When the soft reference associated object is recycled, get returns null.

It's OK to keep it useful and lose it. This kind of thing is suitable for caching. When the memory is not enough, it can be recycled by the garbage collector, which can effectively reduce the risk of memory overflow. Note that the soft reference itself is a strong reference, which also needs to be cleared. You can register ReferenceQueue to monitor the soft reference itself of the associated object that has been recycled for clearing.

byte[] data = new byte[1*1024*1024];
ReferenceQueue<Object> referenceQueue = new ReferenceQueue<>();
SoftReference<byte[]> softReference = new SoftReference<>(data,referenceQueue);
data = null;

try {
    for (int i = 0; i < 10; i++) {
        byte[] temp = new byte[3*1024*1024];
} catch (Throwable t) {
    softReference = null;
VM options:-Xms5m -Xmx5m -XX:+PrintGC

3. Weak reference

Weak references are even weaker. They will be recycled directly during garbage collection. When using WeakReference declaration in Java, one gc will be killed. The rest are similar to soft references.

 byte[] data = new byte[1024*1024];
 WeakReference<byte[]> weakReference = new WeakReference<>(data);
 data = null;

The result is that the associated object can't be obtained after a gc. Note that data is set to null here, otherwise there is a strong reference relationship between data and soft reference.

4. Phantom reference

Virtual reference is the same as no reference. Virtual reference must be used in combination with the ReferenceQueue mentioned earlier. You can use reference queue to monitor whether the associated object will be recycled. You can perform processing operations at this time. The operations are the same as those of soft reference.

byte[] data = new byte[1024*1024];
ReferenceQueue<Object> referenceQueue = new ReferenceQueue<>();
PhantomReference<byte[]> phantomReference = new PhantomReference<> (data,referenceQueue);

The essence of several references is still around the memory recycling mechanism. You can learn some knowledge. Sometimes it may not be directly used in your work, but you can optimize some programs in the right place or at the right time, and it can also help you to read some source code.

Topics: Programming Java jvm REST