Java 强,弱,软,虚 引用

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
    }
}

  

转载于:https://www.cnblogs.com/lick468/p/11568098.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值