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

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


    網站導航:
     
    主站蜘蛛池模板: 午夜国产羞羞视频免费网站| 亚洲国产中文在线视频| 99精品全国免费观看视频| www在线观看播放免费视频日本| 亚洲大香人伊一本线| 亚洲一区AV无码少妇电影☆| 成年女人18级毛片毛片免费观看| 97无码人妻福利免费公开在线视频 | 亚洲伊人久久综合影院| 在线视频免费观看高清| 中文字幕视频免费在线观看| 国产亚洲精品免费| 亚洲精品中文字幕无乱码麻豆| 久久噜噜噜久久亚洲va久| 亚洲精品视频在线看| 国产精品无码一二区免费| 在人线av无码免费高潮喷水| 久久免费的精品国产V∧| 光棍天堂免费手机观看在线观看| 免费人成再在线观看网站| 99久在线国内在线播放免费观看| 72pao国产成视频永久免费| 国产精品亚洲五月天高清| 亚洲一卡2卡3卡4卡5卡6卡| 亚洲国产精品线观看不卡| 亚洲精品午夜在线观看| 亚洲国产综合专区电影在线 | 亚洲jizzjizz在线播放久| 久久亚洲私人国产精品vA| 亚洲va中文字幕无码久久不卡| 亚洲午夜久久久影院伊人| 久久久青草青青国产亚洲免观| 亚洲国产综合精品一区在线播放| 日本特黄a级高清免费大片| 免费黄色一级毛片| 中文字幕乱理片免费完整的| 免费看又黄又爽又猛的视频软件| 免费无码午夜福利片| 一级做a爰片性色毛片免费网站| 亚洲精彩视频在线观看| 亚洲黄色在线电影|