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

  

相关文章:

  • 2022-12-23
  • 2021-10-29
  • 2022-02-04
  • 2021-08-09
  • 2022-01-09
  • 2021-09-03
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-02-20
  • 2021-08-04
  • 2021-11-04
  • 2021-11-08
  • 2021-04-01
  • 2021-11-09
相关资源
相似解决方案