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

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


          網站導航:
           
          主站蜘蛛池模板: 凤冈县| 安化县| 兰溪市| 罗定市| 岳西县| 镇平县| 阿勒泰市| 锦屏县| 渝中区| 营口市| 江西省| 商水县| 兴隆县| 大同市| 五台县| 延庆县| 龙州县| 大埔区| 会泽县| 类乌齐县| 苏尼特右旗| 开封市| 正阳县| 隆尧县| 馆陶县| 临泽县| 米泉市| 甘德县| 罗江县| 兰西县| 清流县| 贡嘎县| 胶南市| 乃东县| 荥阳市| 文昌市| 汉寿县| 新龙县| 离岛区| 安多县| 古丈县|