Reference types in Java

reference type

strong citation

Strong reference is a common reference type, usually objects created by new form are strong reference objects

Object o=new Object();

Objects with strong references are never cleaned up

As long as a strong reference exists, the GC will never recycle the referenced object. The reference associated with the object created by the keyword new is a strong reference. As long as the strong reference still points to an object, it means that the object is still alive and garbage. The collector will not touch, even when the memory is insufficient, the JVM throws an OOM (OutOfMemoryError) exception and will not recycle the strongly referenced reference object.

Soft references, weak references, and virtual references are all stored in the Java.lang.ref package, and the parent class is the Reference class

Reference abstract parent class constructor

//referent为引用指向的对象
Reference(T referent) {
        this(referent, null);
    }
//ReferenceQueue 可以理解为队列,在GC之前,将引用的对象放入Queue中,方便清理引用对象本身
    Reference(T referent, ReferenceQueue<? super T> queue) {
        this.referent = referent;
        this.queue = (queue == null) ? ReferenceQueue.NULL : queue;
    }

for example:

Object o=new Object();
ReferenceQueue queue = new ReferenceQueue();
WeakReference wk = new WeakReference(o,queue);

At this time, wk is a weak reference, pointing to the o object, and o will be cleaned up by GC at a certain time, but the work of the wk object depends on the Queue object. When wk appears in the Queue, it means that the object it points to is invalid and can be cleaned up with confidence

Soft reference: SoftReference

The object affected by the soft reference will not be recycled when the GC operation occurs and the memory is sufficient. When the memory is insufficient, the GC operation is triggered, and the object affected by the soft reference will be recycled.

ReferenceQueue<A> queue = new ReferenceQueue<A>();
        SoftReference<A> w = new SoftReference<A>(new A(), queue);
        System.out.println(w.get());
        System.out.println(w.isEnqueued());
        System.out.println(queue.poll());
        /**
         * main.java.com.tulun.WeakHashMapGY02$A@1e3ac11b
         * false
         * null
         */
        /**
         * 划重点:
         * //此处需要的内存,内存受限,不够用了,因此触发GC,回收软引用对象
         *
         */
        byte[] array = new byte[7*1024*1024+500*1024];
        System.out.println(array[0]);
        System.gc();
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println(w.get());
        System.out.println(w.isEnqueued());
        System.out.println(queue.poll());

Weak reference: WeakReference

Objects affected by weak references. When a GC operation occurs, even if there is sufficient memory space, the GC operation will reclaim the objects affected by weak references.

ReferenceQueue<A> queue = new ReferenceQueue<A>();
        WeakReference<A> w = new WeakReference<A>(new A(), queue);
        System.out.println(w.get());
        System.out.println(w.isEnqueued());
        System.out.println(queue.poll());
//        main.java.com.tulun.WeakHashMapGY02$A@20724356
//        false
//        null
        System.gc();
        System.out.println("---------------------");
        System.out.println(w.get());
        System.out.println(w.isEnqueued());
        System.out.println(queue.poll());

Phantom reference: phantonReference

A virtual reference is the weakest kind of reference relationship. Whether an object has a virtual reference will not affect the object's lifetime, and it is impossible to obtain an instance of an object through a virtual reference.

The existence of virtual references can be used to determine whether the object is recycled

        ReferenceQueue<A> queue = new ReferenceQueue<A>();
        PhantomReference<A> ptr = new PhantomReference<A>(new A(), queue);
        System.gc(); //此处启动GC,回收A对象,queue收到通知,该对象回收了
        if(ptr.isEnqueued()){
            System.out.println("ptr.isEnqueued()");
        }else{
            System.out.println("not ptr.isEnqueued()");
        }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324131822&siteId=291194637