<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 閱讀(129) 評論(0)  編輯  收藏

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


    網站導航:
     
    主站蜘蛛池模板: 国产精品免费综合一区视频| 67pao强力打造高清免费| 天天天欲色欲色WWW免费| 亚洲av无码一区二区三区天堂古代 | 91久久青青草原线免费| 久久精品a亚洲国产v高清不卡| 免费黄网站在线看| 亚洲尹人香蕉网在线视颅| 99久久国产精品免费一区二区| 国产亚洲精品一品区99热| 污视频在线免费观看| 亚洲精品午夜视频| 美女被cao免费看在线看网站| 国产AV旡码专区亚洲AV苍井空| 免费看a级黄色片| 美女被爆羞羞网站在免费观看| 亚洲国产精品尤物yw在线| 中国国语毛片免费观看视频| 亚洲AV午夜成人影院老师机影院| 无码中文字幕av免费放dvd| 亚洲精品午夜久久久伊人| 在线视频观看免费视频18| 久久综合AV免费观看| 亚洲无码一区二区三区| 又黄又大又爽免费视频| 香蕉免费一级视频在线观看| 久久久亚洲AV波多野结衣| 波霸在线精品视频免费观看| 亚洲国产精品自在在线观看| 久久久久久99av无码免费网站| 久久久久久亚洲精品无码| 亚洲色精品aⅴ一区区三区| 最近最新高清免费中文字幕| 亚洲免费网站观看视频| 久久久久亚洲AV成人网人人软件| 久久精品国产免费| 亚洲熟女综合色一区二区三区| 久久亚洲AV无码西西人体| 114一级毛片免费| 一级特黄录像免费播放肥| 亚洲精品二三区伊人久久|