zoukankan      html  css  js  c++  java
  • Java引用对象SoftReference WeakReference PhantomReference(二)

    四、Java对引用的分类

    级别

    什么时候被垃圾回收

    用途

    生存时间

    从来不会

    对象的一般状态

    JVM停止运行时终止

    在内存不足时

    对象简单?缓存

    内存不足时终止

    在垃圾回收时

    对象缓存

    gc运行后终止

    假象

    Unknown

    Unknown

    Unknown

    1、强引用:

    public static void main(String[] args) {

            MyDate date = new MyDate();

            System.gc();

    }

    解释:即使显式调用了垃圾回收,但是用于date是强引用,date没有被回收

    2、软引用:

    public static void main(String[] args) {

            SoftReference ref = new SoftReference(new MyDate());

            drainMemory(); // 让软引用工作

    }

    解释:在内存不足时,软引用被终止,等同于:

    MyDate date = new MyDate();

    //-------------------JVM决定运行-----------------

    If(JVM.内存不足()) {

             date = null;

             System.gc();

    }

    //-------------------------------------------------------------

    3、弱引用:

    public static void main(String[] args) {

            WeakReference ref = new WeakReference(new MyDate());

            System.gc(); // 让弱引用工作

    }

    解释:在JVM垃圾回收运行时,弱引用被终止,等同于:

    MyDate date = new MyDate();

    //------------------垃圾回收运行------------------

    public void WeakSystem.gc() {

             date = null;

             System.gc();

    }

    4、假象引用:

    public static void main(String[] args) {

            ReferenceQueue queue = new ReferenceQueue();

            PhantomReference ref = new PhantomReference(new MyDate(), queue);

            System.gc(); // 让假象引用工作

    }

    解释:假象引用,在实例化后,就被终止了,等同于:

    MyDate date = new MyDate();

    date = null;

    //-------终止点,在实例化后,不是在gc时,也不是在内存不足时--------


    http://hi.baidu.com/mynetbeans/blog/item/d72208fa8d63ac1ba9d31160.html
     

  • 相关阅读:
    delphi string.split 按照任意字符串分割语句
    学习 TTreeView [16]
    学习 TTreeView [15]
    delphi TClientDatset资料
    delphi7 clientdataset 详解
    为TMenuItem增加指针Data属性
    构建一个用于产品介绍的WEB应用
    图片延迟加载技术-Lazyload的应用
    你想不到的压缩方法:将javascript文件压缩成PNG图像存储
    如何更快速加载你的JS页面
  • 原文地址:https://www.cnblogs.com/pony/p/1947941.html
Copyright © 2011-2022 走看看