Java applications strong, weak references, soft references, virtual references

Java applications strong, weak references, soft references, virtual references

Today accidentally saw these four terms, and then had a moment in my mind, I have found forgotten, so writing an article about the record, the saying goes, a good memory as bad written.
Then the first to explain the terms under which four it

A strong reference (StrongReference):

It is a strong reference to the object of our usual new

Object a = new Object();

When out of memory space, java virtual machine rather throws OOM exception with abnormal program termination, it will not rely on random objects recovered have strong references to solve the problem of insufficient memory, if the object when you can not run out by a = null; in this way to play down references; role is to help the garbage collector reclaim this object; this setting by way of a display is null, gc considers the referenced object does not exist, this time gc can recover this object.

Soft references (SoftReference):

If an object is only soft references, and memory enough, garbage recovery period will not recover it; if insufficient memory, the memory will be reclaimed these objects; the main garbage payback period is not recovered, the object may have been program. Soft references can be used to implement memory-sensitive caches.

public class SoftReferenceTest {
    static class HeadObject{
        byte[] bs = new byte[1024 * 1024];
    }

    public static void main(String[] args) {
        SoftReference<HeadObject> softReference = new SoftReference<HeadObject>(new HeadObject());
        List<HeadObject> list = new ArrayList<>();
        while(true){
            if(softReference.get()!= null){
                list.add(new HeadObject());
                System.out.println("list-add");
            }else{
                System.out.println("----软引用被回收");
                break;
            }
            System.gc();
        }
    }
}

The final result of this program code is a plurality of n list-add, and finally - the soft reference is recovered

Weak references (WeakReference):

Weak and soft references cited difference is that: only a weak reference object life cycle is shorter. During the garbage collector thread scans under its jurisdiction memory region, once we found only a weak reference objects, whether sufficient memory is still insufficient, can be recycled. As the thread garbage payback period is relatively low, it is not necessarily one will soon find those objects that have weak references

public static void main(String[] args) throws InterruptedException {
        WeakReference<TestObj> weakReference = new WeakReference(new TestObj());
        System.out.println(weakReference.get() == null);
        System.gc();
        Thread.sleep(1000);
        System.out.println(weakReference.get() == null);
    }

Let Code sleep 1 second, the results of the previous gc is true, the result after gc is false

Virtual reference (WeakReference):

False quote: As the name suggests, relatively empty, non-existent, also known as phantom references, and several other are different, the decision will not phantom reference object life cycle. If an object is a virtual reference only, and then he does not have any references, they are likely to be recovered at any time.
Phantom reference primarily used to track objects recovered garbage collector activity. With reference to a virtual reference difference between the soft and weak reference is that: the virtual reference and reference must queue (the ReferenceQueue) used in combination. When the garbage collector is ready recovery of an object, it is found that if there is a virtual reference, will be recovered before the memory, this reference is added to the virtual queue associated with a reference.

to sum up:

Fourth reference level from high to low:
strong references> soft reference> weak reference> phantom reference
in line to see a relatively plain words to describe these four references:

  • Strong Quote: like a boss (OOM) of his own son, can not do anything in the company, but do not always take up the company's resources for his own work, remember to run the company after the sister, let them to work (to know how to release resources) or else the company will likely Beat.
  • Soft references: a bit like the boss (OOM) relatives, the poor performance of the company is likely to be dismissed, even if you complain that he (call GC) to work to see the film, but as long as not to see the boss (JVM is detected) is not It will be dismissed (virtual machine is recovered).
  • Weak references: is an ordinary employee, usually if the poor performance will be dismissed (subject no other cases cited), others encountered complaints (call GC) to work to see the film, it is affirmed dismissal (virtual machine is recovered) .
  • False quote: This stock is estimated that an intern with the temporary workers, something happens when you think, no matter when, seconds seconds to come up to the top of the pot, dismissed.

Guess you like

Origin blog.csdn.net/u010996565/article/details/85303438