强引用、软引用、弱引用、虚引用

2021-03-16  本文已影响0人  i小雨
图片.png

强引用:
就算OOM也不会回收
Book b1 = new Book();//b1就是一个强引用

图片.png

软引用:
内存够用的时候保留,不够用就回收

1、测试内存足够的情况下软引不会回收:

public static void softRef_Memory_Enough() {
        Object o1 = new Object();
        SoftReference<Object> softReference = new SoftReference<>(o1);
        
        System.out.println(o1);//java.lang.Object@15db9742
        System.out.println(softReference.get());//java.lang.Object@15db9742
        
        o1 = null;
        System.gc();
        
        System.out.println(o1);//null
        System.out.println(softReference.get());//java.lang.Object@15db9742   内存足够的情况下软引用不会回收
}
*******************执行方法后的结果:*********************
java.lang.Object@15db9742
java.lang.Object@15db9742
null
java.lang.Object@15db9742

2、测试内存不足的情况下软引用回收:

   /*
    * 内存不够用就会回收
          JVM 配置参数,将内存配置为5M,导致OOM
          -Xms5m -Xmx5m -XX:+PrintGCDetails
     */
    public static void softRef_Memory_NotEnough() {
        Object o1 = new Object();
        SoftReference<Object> softReference = new SoftReference<>(o1);
        
        System.out.println(o1);
        System.out.println(softReference.get());
        
        o1 = null;
        System.gc();
        
        try {
            byte[] bytes = new byte[30*1024*1024];//30M
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            System.out.println(o1);
            System.out.println(softReference.get());    
        }
    }
*******************执行方法后的结果:*********************
java.lang.Object@15db9742
java.lang.Object@15db9742
null
null
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
    at com.yy.java.reference.softreference.TestSoftReference.softRef_Memory_NotEnough(TestSoftReference.java:43)
    at com.yy.java.reference.softreference.TestSoftReference.main(TestSoftReference.java:9)

软引用的应用场景:

图片.png

弱引用:
不管内存是否够,只要gc,就会回收。

 /*
  * 不管内存是否够,只要gc,就会回收
  */
public static void weakRef() {
        Object o1 = new Object();
        WeakReference<Object> weakReference = new WeakReference<>(o1);
        
        System.out.println(o1);
        System.out.println(weakReference.get());
        
        o1 = null;
        System.gc();
        
        System.out.println(o1);
        System.out.println(weakReference.get());    
    }
*******************执行方法后的结果:*********************
java.lang.Object@15db9742
java.lang.Object@15db9742
null
null

WeakHashMap测试:
WeakHashMap可以用于高速缓存。

public class TestWeakHashMap {

    public static void main(String[] args) {
        myHashMap();
        System.out.println("=======================");
        myWeakHashMap();
    }

    private static void myHashMap() {
        HashMap<Integer, String> hashMap = new HashMap<>();
        
        Integer key = new Integer(1);
        String value = "hashMap";
        hashMap.put(key, value);
        System.out.println(hashMap);
        
        key=null;
        System.out.println(hashMap);
        
        System.gc();
        System.out.println(hashMap+"\t"+hashMap.size());
        
    }
    
    private static void myWeakHashMap() {
        WeakHashMap<Integer, String> weakHashMap = new WeakHashMap<>();
        
        Integer key = new Integer(2);
        String value = "weakHashMap";
        weakHashMap.put(key, value);
        System.out.println(weakHashMap);
        
        key=null;
        System.out.println(weakHashMap);
        
        System.gc();
        System.out.println(weakHashMap+"\t"+weakHashMap.size());
        
    }
}
*******************结果:*********************
{1=hashMap}
{1=hashMap}
{1=hashMap} 1
=======================
{2=weakHashMap}
{2=weakHashMap}
{}  1

不知道为什么WeakHashMap的key置空了,再gc后内容为空,但是size为1。请看到的大佬指教,谢谢。

虚引用:
也叫做幽灵引用

图片.png

引用队列结合WeakReference测试:

public class TestReferenceQueue {

    public static void main(String[] args) throws InterruptedException {
        Object o1 = new Object();
        ReferenceQueue<Object> referenceQueue = new ReferenceQueue<>();//引用队列
        WeakReference<Object> weakReference = new WeakReference<>(o1,referenceQueue);
        
        System.out.println(o1);
        System.out.println(weakReference.get());
        System.out.println(referenceQueue.poll());
        
        System.out.println("===============");
        o1 = null;
        System.gc();
        Thread.sleep(1000);
        
        System.out.println(o1);
        System.out.println(weakReference.get());
        System.out.println(referenceQueue.poll());
    }
}
*******************结果:*********************
java.lang.Object@15db9742
java.lang.Object@15db9742
null
===============
null
null
java.lang.ref.WeakReference@6d06d69c
上一篇下一篇

猜你喜欢

热点阅读