Java内存泄露的理解与解决

发表于:2011-12-16 09:47

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:henryyang    来源:51Testing软件测试网采编

  附SoftHashmap 的源码一份,相信看过之后,大家会对 Reference 机制的应用有更深入的理解。

  1. package  com. *** .widget;  
  2.     // : SoftHashMap.java    
  3.     import  java.util. * ;   
  4.     import  java.lang.ref. * ;   
  5.     import  android.util.Log;  
  6.      
  7.     public   class  SoftHashMap  extends  AbstractMap  {   
  8.       /**  The internal HashMap that will hold the SoftReference.  */    
  9.       private   final  Map hash  =   new  HashMap();   
  10.       /**  The number of "hard" references to hold internally.  */    
  11.       private   final   int  HARD_SIZE;   
  12.       /**  The FIFO list of hard references, order of last access.  */    
  13.       private   final  LinkedList hardCache  =   new  LinkedList();   
  14.       /**  Reference queue for cleared SoftReference objects.  */    
  15.       private  ReferenceQueue queue  =   new  ReferenceQueue();   
  16.       // Strong Reference number   
  17.       public  SoftHashMap()  {  this ( 100 ); }    
  18.       public  SoftHashMap( int  hardSize)  { HARD_SIZE  =  hardSize; }    
  19.        
  20.       public  Object get(Object key)  {   
  21.        Object result  =   null ;   
  22.         //  We get the SoftReference represented by that key    
  23.        SoftReference soft_ref  =  (SoftReference)hash.get(key);   
  24.         if  (soft_ref  !=   null )  {   
  25.           //  From the SoftReference we get the value, which can be   
  26.           //  null if it was not in the map, or it was removed in   
  27.           //  the processQueue() method defined below    
  28.         result  =  soft_ref.get();   
  29.           if  (result  ==   null )  {   
  30.             //  If the value has been garbage collected, remove the   
  31.             //  entry from the HashMap.    
  32.            hash.remove(key);   
  33.          }   else   {   
  34.             //  We now add this object to the beginning of the hard   
  35.             //  reference queue.  One reference can occur more than   
  36.             //  once, because lookups of the FIFO queue are slow, so   
  37.             //  we don't want to search through it each time to remove   
  38.             //  duplicates.   
  39.               // keep recent use object in memory   
  40.            hardCache.addFirst(result);   
  41.             if  (hardCache.size()  >  HARD_SIZE)  {   
  42.               //  Remove the last entry if list longer than HARD_SIZE    
  43.              hardCache.removeLast();   
  44.            }    
  45.          }    
  46.        }    
  47.         return  result;   
  48.      }    
  49.     
  50.       /**  We define our own subclass of SoftReference which contains   
  51.       not only the value but also the key to make it easier to find   
  52.       the entry in the HashMap after it's been garbage collected.  */    
  53.       private   static   class  SoftValue  extends  SoftReference  {   
  54.         private   final  Object key;  //  always make data member final    
  55.         /**  Did you know that an outer class can access private data   
  56.         members and methods of an inner class?  I didn't know that!   
  57.         I thought it was only the inner class who could access the   
  58.         outer class's private information.  An outer class can also   
  59.         access private members of an inner class inside its inner   
  60.         class.  */    
  61.         private  SoftValue(Object k, Object key, ReferenceQueue q)  {   
  62.           super (k, q);   
  63.           this .key  =  key;   
  64.        }    
  65.      }    
  66.     
  67.       /**  Here we go through the ReferenceQueue and remove garbage   
  68.       collected SoftValue objects from the HashMap by looking them   
  69.       up using the SoftValue.key data member.  */    
  70.       public   void  processQueue()  {   
  71.        SoftValue sv;   
  72.         while  ((sv  =  (SoftValue)queue.poll())  !=   null )  {   
  73.             if (sv.get() ==   null ) {  
  74.                Log.e( " processQueue " ,  " null " );  
  75.            } else {  
  76.                Log.e( " processQueue " ,  " Not null " );  
  77.            }   
  78.          hash.remove(sv.key);  //  we can access private data!   
  79.          Log.e( " SoftHashMap " ,  " release  "   +  sv.key);  
  80.        }    
  81.      }    
  82.       /**  Here we put the key, value pair into the HashMap using   
  83.       a SoftValue object.  */    
  84.       public  Object put(Object key, Object value)  {   
  85.        processQueue();  //  throw out garbage collected values first    
  86.        Log.e( " SoftHashMap " ,  " put into  "   +  key);  
  87.         return  hash.put(key,  new  SoftValue(value, key, queue));   
  88.      }    
  89.       public  Object remove(Object key)  {   
  90.        processQueue();  //  throw out garbage collected values first    
  91.         return  hash.remove(key);   
  92.      }    
  93.       public   void  clear()  {   
  94.        hardCache.clear();   
  95.        processQueue();  //  throw out garbage collected values    
  96.       hash.clear();   
  97.     }    
  98.      public   int  size()  {   
  99.       processQueue();  //  throw out garbage collected values first    
  100.        return  hash.size();   
  101.     }    
  102.      public  Set entrySet()  {   
  103.        //  no, no, you may NOT do that!!! GRRR    
  104.        throw   new  UnsupportedOperationException();   
  105.     }   
  106.   }

44/4<1234
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号