首页 > 编程语言 > 详细

Java 强,弱,软,虚 引用

时间:2019-09-22 17:07:27      阅读:107      评论:0      收藏:0      [点我收藏+]
import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;

public class TestGC {
    /**
     *
     * 软引用   当内存满的时候,才会回收软引用指向的对象
     * 弱引用  每次进行垃圾回收时,不论内存是否满,都是回收弱引用指向的对象
     *
     * @param args
     */
    public static void main(String[] args) {
        String str = new String("asdasd"); //强引用
        SoftReference<String> softReference = new SoftReference<String>(str); //软引用
        str = null; // 去掉强引用
        System.gc(); //垃圾回收器进行回收
        System.out.println(softReference.get()); // asdasd

        String abc = new String("asdas"); // 强引用
        WeakReference<String> weakReference = new WeakReference<String>(abc);//弱引用
        abc = null;// 去掉强引用
        System.gc(); // 垃圾回收器进行回收
        System.out.println(weakReference.get()); // null
    }
}

  

Java 强,弱,软,虚 引用

原文:https://www.cnblogs.com/lick468/p/11568098.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!