<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免费观看| 亚洲人成77777在线播放网站| 免费精品久久天干天干| 亚洲综合无码一区二区| 国产片AV片永久免费观看| 亚洲国产熟亚洲女视频| 免费A级毛片无码A| 91精品全国免费观看青青| 亚洲精品电影在线| 好吊妞在线新免费视频| 一级中文字幕免费乱码专区| 亚洲AV无码一区二区三区DV| 国产成人免费在线| 亚洲日韩精品无码专区加勒比 | 亚洲日韩国产二区无码| 国产一级一片免费播放i| 久久一区二区免费播放| 亚洲高清无在码在线无弹窗| 成人毛片免费播放| 成人妇女免费播放久久久| 亚洲综合综合在线| 亚洲国产成人久久综合区| 无码国产精品一区二区免费式芒果 | 久久精品国产大片免费观看| 亚洲一区二区三区乱码在线欧洲| 四虎AV永久在线精品免费观看| 日本黄色动图免费在线观看| 亚洲熟女www一区二区三区|