<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆-12  評論-0  文章-1  trackbacks-0
    The main difference between SoftReference and WeakReference is that GC uses different algorithm to determine when to reclaim the objects they refer to, they can be used interchangablely in most situation.

           
            // soft reference
            o = new Object();
            ReferenceQueue
    <Object> sq = new ReferenceQueue<Object>();
            SoftReference
    <Object> sf = new SoftReference<Object>(o, sq);
            System.out.println(sf.get());
            o 
    = null;
            
            System.gc();
            r 
    = (Reference) sq.poll();
            
    if (r != null) {
                System.out.println(r.get());
            }

            // weak reference
            Object o = new Object();
            ReferenceQueue
    <Object> q = new ReferenceQueue<Object>();
            WeakReference
    <Object> w = new WeakReference<Object>(o, q);
            System.out.println(w.get());
            o 
    = null;
            System.gc();
            
            Reference r 
    = (Reference) q.poll();
            
    if (r != null) {
                System.out.println(r.get());
            }

            
    // phantom reference
            o = new Object();
            ReferenceQueue
    <Object> pq = new ReferenceQueue<Object>();
            PhantomReference
    <Object> pf = new PhantomReference<Object>(o, sq);
            System.out.println(pf.get());
            o 
    = null;
            
            System.gc();
            r 
    = (Reference) pq.poll();
            
    if (r != null) {
                System.out.println(r.get());
            }
           


    from http://www.ibm.com/developerworks/library/j-refs/

    The SoftReference class

    A typical use of the SoftReference class is for a memory-sensitive cache. The idea of a SoftReference is that you hold a reference to an object with the guarantee that all of your soft references will be cleared before the JVM reports an out-of-memory condition. The key point is that when the garbage collector runs, it may or may not free an object that is softly reachable. Whether the object is freed depends on the algorithm of the garbage collector as well as the amount of memory available while the collector is running.

    The WeakReference class

    A typical use of the WeakReference class is for canonicalized mappings. In addition, weak references are useful for objects that would otherwise live for a long time and are also inexpensive to re-create. The key point is that when the garbage collector runs, if it encounters a weakly reachable object, it will free the object the WeakReference refers to. Note, however, that it may take multiple runs of the garbage collector before it finds and frees a weakly reachable object.

    The PhantomReference class

    The PhantomReference class is useful only to track the impending collection of the referring object. As such, it can be used to perform pre-mortem cleanup operations. A PhantomReference must be used with the ReferenceQueue class. The ReferenceQueue is required because it serves as the mechanism of notification. When the garbage collector determines an object is phantomly reachable, the PhantomReference object is placed on its ReferenceQueue. The placing of the PhantomReference object on the ReferenceQueue is your notification that the object the PhantomReference object referred to has been finalized and is ready to be collected. This allows you to take action just prior to the object memory being reclaimed.

    posted on 2012-01-08 13:06 Sam Zheng 閱讀(126) 評論(0)  編輯  收藏

    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 成人免费在线观看网站| 国产成人精品免费大全| 最近免费视频中文字幕大全| 亚洲综合色自拍一区| 美女被爆羞羞网站免费| 伊在人亚洲香蕉精品区麻豆| 国产天堂亚洲国产碰碰| 亚洲国产香蕉人人爽成AV片久久| 成人在线免费视频| 中文字幕亚洲图片| 中文字幕无码免费久久| 亚洲国产精品久久久久网站| 亚洲欧洲免费视频| 亚洲一区二区三区精品视频| 91免费资源网站入口| 亚洲成a人无码亚洲成www牛牛| 国产成人免费一区二区三区| 一级毛片人与动免费观看| 久久精品国产亚洲AV不卡| 国产午夜无码精品免费看| 亚洲成人福利网站| 日本免费一区二区三区最新vr| 黄色三级三级免费看| 亚洲日韩乱码中文无码蜜桃臀网站 | 亚洲AV永久无码精品放毛片| www亚洲一级视频com| 国产免费爽爽视频在线观看 | 国产亚洲av人片在线观看| 国产麻豆成人传媒免费观看| 亚洲视频网站在线观看| 成年女人18级毛片毛片免费| 免费无码国产V片在线观看| 亚洲AV日韩AV永久无码久久| 成人毛片手机版免费看| 免费国产污网站在线观看不要卡| 亚洲精品无码久久一线| 日日AV拍夜夜添久久免费| 中文字幕无码免费久久| 亚洲看片无码在线视频| 国产亚洲精品线观看动态图| 国产又大又粗又长免费视频 |