JVM reference type

 

1, strong references

Strong reference, in our development work among ubiquitous. If an object has a strong reference, then we often wear similar clothes and other essential supplies ah, we certainly would not put him away, the same jvm garbage collector does not reclaim it. When out of memory space, java virtual machine rather throws OOM exception will not be recovered with strong references to free memory. We can assign objects displayed is null, gc considered the reference object does not exist, then you can recover the object. Exactly when to collect it depends gc algorithm. That method has a strong internal reference, this reference is stored in the stack, while the real reference content (Object) stored on the heap. When this method is completed it will exit the stack method, the citation references do not exist, the Object will be recycled.

 

2, soft references

 SoftReference: is stronger than references cited weakening, so that the object can exempt a number of garbage collection, soft references are usually in memory sensitive programs, such as cache area, mybatis is used in this way. He is characterized by the existence of adequate when not recycled, is recycled when out of memory. 

 

package com.jalja.java.reference;

import java.lang.ref.SoftReference;

/**
 * @Auther: XL
 * @Date: 2019/12/24 07:50
 * @Description:
 */
public class SoftReferenceTest {
    public static void main(String [] args) throws Exception{
        //softReferenceAmple();
        softReferenceNotAmple();
    }

    /**
     * 内存充足
     */
    private static void  softReferenceAmple(){
        Object o=new Object();
        SoftReference<Object> reference=new SoftReference<>(o);
        System.out.println(o);
        System.out.println(reference.get());

        o=null;
        System.gc();
        System.out.println(o);
        System.out.println(reference.get());
    }

    /**
     * 内存不足就回收
     */
    private static void  softReferenceNotAmple(){
        Object o=new Object();
        SoftReference<Object> reference=new SoftReference<>(o);
        System.out.println(o);
        System.out.println(reference.get());
        o=null;
        try {
            byte[]bytes=new byte[30*1024*1024];
        }finally {
            System.out.println(o);
            System.out.println(reference.get());
        }
    }
}
View Code

3, weak references

 WeakReference: regardless of whether sufficient memory, as long as the object of some weak references GC occurred is recovered

package com.jalja.java.reference;

import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;
import java.util.WeakHashMap;

/**
 * @Auther: XL
 * @Date: 2019/12/24 08:02
 * @Description:
 */
public class WeakReferenceTest {
    public static void main(String [] args) throws Exception{
        weakReferenceTest();
    }

    /**
     * 内存充足
     */
    private static void weakReferenceTest(){
        Object o=new Object();
        WeakReference<Object> reference=new WeakReference<>(o);
        System.out.println(o);
        System.out.println(reference.get());

        o=null;
        System.gc();
        System.out.println("==========GC=========");
        System.out.println(o);
        System.out.println(reference.get());
    }
    private static void weakHashMapTest(){
        WeakHashMap<String,Object> weakHashMap=new WeakHashMap<>();

    }
}
View Code

 

 WeakHashMap:

 

4, phantom reference

 PhantomReference:

 

    public static void main(String [] args) throws Exception{
        Object o=new Object();
        ReferenceQueue<Object> queue=new ReferenceQueue();
        PhantomReference<Object> reference=new PhantomReference<>(o,queue);
        System.out.println(o);
        System.out.println(reference.get());
        System.out.println(queue.poll());

        o=null;
        System.gc();
        System.out.println("==========GC=========");
        System.out.println(o);
        System.out.println(reference.get());
        System.out.println(queue.poll());
    }

引用队列:ReferenceQueue

 

package com.jalja.java.reference;

import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference;

/**
 * @Auther: XL
 * @Date: 2019/12/24 08:28
 * @Description: ReferenceQueue 引用队列,在GC后悔保留对象的引用
 */
public class ReferenceQueueTest {
    public static void main(String [] args) throws Exception{
        Object o=new Object();
        ReferenceQueue<Object> queue=new ReferenceQueue();
        WeakReference<Object> reference=new WeakReference<>(o,queue);
        System.out.println(o);
        System.out.println(reference.get());
        System.out.println(queue.poll());

        o=null;
        System.gc();
        System.out.println("==========GC=========");
        System.out.println(o);
        System.out.println(reference.get());
        System.out.println(queue.poll());
    }
}

 

Guess you like

Origin www.cnblogs.com/jalja/p/12057700.html
Recommended