<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)  編輯  收藏

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


    網站導航:
     
    主站蜘蛛池模板: 日本免费人成网ww555在线| 麻豆安全免费网址入口| 最近中文字幕大全免费视频| 亚洲色婷婷六月亚洲婷婷6月 | 亚洲综合色成在线播放| 成人午夜免费视频| 亚洲天堂在线视频| 91视频免费观看| 亚洲国产精品自在在线观看| 99ee6热久久免费精品6| 亚洲视频免费观看| 久久精品免费一区二区| 亚洲国产成人精品激情| 免费看美女被靠到爽| 美女扒开尿口给男人爽免费视频| 亚洲午夜AV无码专区在线播放 | 亚洲中文字幕无码爆乳av中文| fc2成年免费共享视频网站| 在线亚洲人成电影网站色www | 18女人水真多免费高清毛片| 亚洲精品国产免费| 毛片免费视频在线观看| 免费精品国产自产拍在线观看| 亚洲精品制服丝袜四区| 蜜臀98精品国产免费观看| 国产精品亚洲自在线播放页码| 国产三级免费电影| 三年片在线观看免费| 亚洲色大成网站www永久| 毛片高清视频在线看免费观看| 小说专区亚洲春色校园| 亚洲AV永久无码精品成人| 成人免费在线看片| 日韩精品视频在线观看免费| 亚洲国产精品自在线一区二区| 天天摸天天碰成人免费视频| 99久久精品毛片免费播放| 亚洲人成电影院在线观看| 亚洲欧洲日本在线| 国产92成人精品视频免费| 一级日本高清视频免费观看 |