Go homepage(回首页) Upload pictures (上传图片) Write articles (发文字帖)
The author:(作者)delvpublished in(发表于) 2014/1/6 8:46:54 .net回收机制的使用_[Asp.Net教程]
Class Expensive{
Static Stack pool = new Stack();
Public static Expensive GetObjectFromPool(){
Retun (Expensive) pool.Pop();
}
Public static void ShutdownThePool(){
Pool = null;
Public Expensive(){
//先构造对象
Pool.push(this);
Finalize (){
If(pool!=null){
GC.RegisterForFinally(this;)//先把他叫醒挨宰
Pool.push(this);//将“清醒的”对象加入到对象池中,让他起死回生
Class app{
Static void main(){
New expensive();
……
Expensive e = Expensiv. GetObjectFromPool();
//下面就可以使用e了
Expensive.shutdownThepool();//关闭应用程序前,先关闭对象池,否则会在内存中留下“孔洞”,因为Finalize已经被重写了
来源:网络
赞