If a terminated object resides in memory and is not used, it will occupy a lot of system resources, so the .net garbage collector will put it in the abort queue. Once the time is right, the object will be awakened. And call its finalize method to completely terminate it. However, using this mechanism, you can put the resource-consuming giant class into an object pool and reuse it throughout the entire lifetime of the program (end mark is that the clr thinks there is no root in the process for that application). We call these giant classes Expensive objects. Let's discuss the structure of the object pool that manages Expensive objects.
Class Expensive{
Static Stack pool = new Stack();
Public static Expensive GetObjectFromPool(){
Retun (Expensive) pool.Pop();
}
Public static void ShutdownThePool(){
Pool = null;
}
Public Expensive(){
//Construct the object
Pool.push(this) first;
}
Finalize (){
If(pool!=null){
GC.RegisterForFinally(this;)/ /First wake him up and kill him
Pool.push(this); //Add the "awake" object to the object pool and bring him back to life
}
}
}
Class app{
Static void main(){
New expensive();
......
Expensive e = Expensiv. GetObjectFromPool();
//You can use e below.Expensive.shutdownThepool
();//Before closing the application, close the object pool , otherwise it will leave a "hole" in the memory, because Finalize has been rewritten
}
}