弱引用 WeakReference

转载自: http://www.rainsts.net/article.asp?id=78

在程序设计中我们经常会进行一些全局缓存设计,诸如使用静态或者全局根字段来引用某个对象,以便一次创建多次使用。

如:
None.gif    class  BigData
ExpandedBlockStart.gifContractedBlock.gif  
dot.gif {
ExpandedBlockEnd.gif  }

None.gif
None.gif  
class  Program
ExpandedBlockStart.gifContractedBlock.gif  
dot.gif {
InBlock.gif    
static BigData cache;
InBlock.gif
InBlock.gif    
public static BigData DataCache
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      
get 
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif        
if (cache== null) cache= new BigData();
InBlock.gif        
return cache;
ExpandedSubBlockEnd.gif      }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif  }

但是这样做在某些时候会存在一些弊端,如:
1. 当dataCache并没有被频繁使用,甚至因为某些原因仅仅被使用了一次时会造成内存资源的浪费。
2. 由于GC只能回收不可达对象,因此即便内存不足,GC也无法回收这些闲置资源。

这时建议你使用 WeakReference 来重构你的程序,以便获得更好的系统性能。
WeakReference :“弱引用”,即在引用对象的同时仍然允许对该对象进行垃圾回收。
使用弱引用后,不应该再使用强引用,有关细节可以参考SDK帮助文档。
None.gif    class  BigData
ExpandedBlockStart.gifContractedBlock.gif  
dot.gif {
InBlock.gif    
~BigData()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      Console.WriteLine(
"Destorydot.gif");
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public void Test()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      Console.WriteLine(
"Test");
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif  }

None.gif
None.gif  
class  Program
ExpandedBlockStart.gifContractedBlock.gif  
dot.gif {
InBlock.gif    
static WeakReference cache = new WeakReference(null);
InBlock.gif
InBlock.gif    
public static BigData DataCache
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      
get 
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif        BigData data 
= cache.Target as BigData;
InBlock.gif        
if (data == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif          data 
= new BigData();
InBlock.gif          cache.Target 
= data;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
return data;
ExpandedSubBlockEnd.gif      }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
static void Main(string[] args) 
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif
InBlock.gif      DataCache.Test();
InBlock.gif      DataCache.Test();
InBlock.gif
InBlock.gif      GC.Collect();
InBlock.gif      DataCache.Test();
ExpandedSubBlockEnd.gif    }
 
ExpandedBlockEnd.gif  }


改进后的程序,我们依旧可以实现我们缓存的目的,而GC也可以在合时的时候释放cache占用的内存。
.NET中的缓存功能多采用了类似的设计。
当然并非要求所有的场合都适合使用弱引用。

补充:

弱引用分为"短弱引用(Short Week Reference)"和"长弱引用(Long Week Reference)",其区别是长弱引用在对象的Finalize方法被GC调用后依然追踪对象。基于安全考虑,不推荐使用长弱引用。

因此建议使用
WeakReference wr = new WeakReference(object);

WeakReference wr = new WeakReference(object, false);

转载于:https://www.cnblogs.com/netflu/archive/2006/06/12/423720.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值