[Java] Records about strong, soft, weak, and phantom references

First of all, don't be intimidated by some terms. In fact, they are all things that exist and are reasonable. The reference itself is easy to understand. The data of the reference type stores the memory address of the actual object. When garbage collection, it depends on whether the object has a reference. Java does not require developers to allocate memory and release memory, but it can handle the life cycle of related objects through four reference types, and cooperate with jvm for garbage collection.

1. Strong references

Normally creating objects and assigning variables belong to strong reference types. Strong reference types will not be recycled during garbage collection. OutOfMemoryError will be thrown directly when the memory is insufficient.



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

For example, in the above example, jvm specifies a maximum heap memory of 1m, and the program needs to create a 2m thing, and an OOM error will be thrown directly when the program runs. When the reference no longer needs the associated object, null assignment can be performed to facilitate jvm garbage collection.

2. Soft Reference (SoftReference)

Objects with soft references will not be recycled when the memory is sufficient, and will be recycled before OOM occurs. Use SoftReference to declare a soft reference in Java, and use the get method to return a strong reference to the object. When the soft reference associated object is recycled, get returns null.

Keep it useful, or lose it. This kind of stuff 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. But note that the soft reference itself is a strong reference and also needs to be cleared. You can perform the clearing operation by registering the ReferenceQueue to monitor the reclaimed soft reference of the associated object.


byte[] data = new byte[1*1024*1024];ReferenceQueue<Object> referenceQueue = new ReferenceQueue<>();SoftReference<byte[]> softReference = new SoftReference<>(data,referenceQueue);data = null;System.out.println("before:"+softReference.get());
try {    for (int i = 0; i < 10; i++) {        byte[] temp = new byte[3*1024*1024];        System.out.println("processing:"+softReference.get());    }} catch (Throwable t) {    System.out.println("after:"+softReference.get());    t.printStackTrace();}while(referenceQueue.poll()!=null){    System.out.println("self:"+softReference);    softReference.clear();    softReference = null;    System.out.println("last:"+softReference);}VM options:-Xms5m -Xmx5m -XX:+PrintGC

3. Weak Reference (WeakReference)

Weak references are even weaker. They will be recycled directly during garbage collection. WeakReference is used in Java to declare that gc will be killed once, and the rest is similar to soft references.


byte[] data = new byte[1024*1024];WeakReference<byte[]> weakReference = new WeakReference<>(data);data = null;System.out.println("before:"+weakReference.get());System.gc();System.out.println("after:"+weakReference.get());

The result is that you can't get the associated object after a gc. Note that the data is set to null here, otherwise there is a strong reference relationship between data, and the same is true for soft references.

4. Phantom Reference (PhantomReference)

Virtual references are the same as no references. Virtual references must be used in conjunction with the aforementioned ReferenceQueue. The reference queue can be used to monitor whether the associated objects are about to be recycled. Processing operations can be performed at this time. The operations are the same as soft references and weak references.


byte[] data = new byte[1024*1024];ReferenceQueue<Object> referenceQueue = new ReferenceQueue<>();PhantomReference<byte[]> phantomReference = new PhantomReference<> (data,referenceQueue);System.out.println(phantomReference.get());

The essence of several kinds of references is still around the memory recovery mechanism. Some knowledge may not be used directly in the work sometimes, but some programs can be optimized at the appropriate place or timing, and it can also be useful when reading some source code. Helping effect.


Guess you like

Origin blog.51cto.com/15060464/2638247