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

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


    網站導航:
     
    主站蜘蛛池模板: 少妇性饥渴无码A区免费 | 亚洲成人福利在线| 99久久99久久精品免费观看| 亚洲人成网站在线播放vr| 日韩在线视频免费| 亚洲中文字幕第一页在线| 中文在线观看免费网站| 国产AV无码专区亚洲AV毛网站| 免费看一区二区三区四区| 亚洲成av人在线视| 最近中文字幕大全免费视频| 亚洲伊人久久大香线焦| 成人免费毛片内射美女APP| 久久综合久久综合亚洲| 国产又长又粗又爽免费视频| 国产亚洲精品精品精品| 亚洲精品视频免费观看| 免费一区二区无码东京热| 亚洲精品国产成人99久久| 黄色片在线免费观看| 亚洲精品国产av成拍色拍| 免费在线黄色网址| 丁香花在线视频观看免费| 亚洲第一二三四区| 国产又粗又猛又爽又黄的免费视频| 污网站在线观看免费| 亚洲av日韩av无码黑人| 99热在线精品免费全部my| 美女羞羞免费视频网站| 亚洲国产精品一区第二页| 很黄很黄的网站免费的| 国产一区二区三区亚洲综合| 中文字幕亚洲日韩无线码| 亚洲一级毛片免费看| 美女18一级毛片免费看| 久久精品国产亚洲av麻豆| 全免费a级毛片免费看不卡| 国产色爽免费无码视频| 亚洲熟妇无码AV| 亚洲av无码一区二区三区乱子伦| 久久这里只有精品国产免费10|