All articles(网络文学目录) All Pictures(图片目录) All Softwares(软件目录)

 
.net回收机制的使用_[Asp.Net教程]

Writer: delv Article type: Programming skills(编程技巧) Time: 2014/1/6 8:46:54 Browse times: 356 Comment times: 0

.net回收机制的使用_[Asp.Net教程]


Head photo

Go homepage
Upload pictures
Write articles

.net回收机制的使用_[Asp.Net教程] 一个被终止的对象常驻内存重而不被使用的话,会占用很多系统资源,于是.net的垃圾回收器就会将它放在中止化队列中,一旦时机成熟,该对象就会被唤醒,并调用他的finalize方法,来彻底的终结他,但是利用这一机制,就可以将耗费资源的巨型类放入到一个对象池中,在程序的整个生存期内重复的使用他(结束的标志是clr认为在进程中不存在任何该应用程序的根)。我们称这些巨型类为Expensive对象,下面来讨论一下管理Expensive对象的对象池的结构。

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已经被重写了

}

}

来源:网络





There are 0 records,
Comment:
Must be registered users to comment(必须是注册用户才能发表评论)

Disclaimer Privacy Policy About us Site Map
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.