<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视频在线精品免费观看6| 国产免费福利体检区久久| aⅴ免费在线观看| 亚洲AV日韩精品久久久久久 | 成人性生交大片免费看好| 亚洲国产精品嫩草影院久久 | 中文字幕亚洲图片| www免费黄色网| 国产亚洲综合网曝门系列| 免费无码作爱视频| 亚洲VA成无码人在线观看天堂| 高清永久免费观看| 午夜影视日本亚洲欧洲精品一区| 免费毛片在线看不用播放器| 久久亚洲成a人片| 国产成人精品免费视频网页大全| 91亚洲精品麻豆| 在线精品免费视频无码的| 另类小说亚洲色图| 亚洲人成人网站色www| 99在线观看精品免费99| 精品久久亚洲中文无码| 日本免费一区二区三区最新| 一级做a爰片性色毛片免费网站| 国产成人亚洲影院在线观看| 久久久免费的精品| 亚洲综合av一区二区三区| 免费亚洲视频在线观看| 一个人免费视频观看在线www| 久久久久久亚洲Av无码精品专口| 中文字幕影片免费在线观看| 特级aaaaaaaaa毛片免费视频| 国产亚洲婷婷香蕉久久精品| 美女被免费喷白浆视频| 粉色视频免费入口| 老汉色老汉首页a亚洲| 成人av免费电影| 日本免费A级毛一片| 亚洲中文字幕无码爆乳| 亚洲日韩av无码| 青苹果乐园免费高清在线|