Garbage Recycling Programming - MS.NET Document Read Notes

zhaozj2021-02-16  49

? Net Framework's garbage collector implicitly implemented all necessary memory management tasks. However, when you create an object of the encapsulated unmanaged resource, you must explicitly release them after using these unmanaged resources in your application.

NET Framework

Object.Finalize method

It allows objects to properly clean their unmanaged resources when the garbage collector is recycled. By default, the Finalize method does not perform any operations. If you want the garbage collector to perform cleanup operations to the object before recycling objects, you must rewrite the Finalize method in the class. C # and hosted extensions provide destructor as a simplified mechanism for writing termination code. The destructor automatically generates the Finalize method and the call to the base class.

The garbage collector tracks an object with a Finalize method using an internal structure called "Target Queue". Each time your application creates an object with a Finalize method, the garbage collector places an item that points to the object in the termination queue. All objects that need to call their termination code before the garbage collector recycled their memory, contain items in the termination queue.

The memory used by the Finalize method is used to recover the memory needs to be recycled at least two garbage. When the garbage collector performs recycling, it only reclaims memory of the unacceptable object without the termination. At this time, it cannot recover the irrevacular objects with a terminator. It is changed to remove these objects from the termination queue and placed them in the list of objects labeled as the termination. The items in this list point to the hosted stacks are prepared to call their termination code. Garbage Recycler calls the Finalize method for the objects in this list, then removes these items from the list. Later garbage recycling will determine the object of the termination is indeed garbage, as items in the list of tabular objects are no longer pointing them. In the subsequent garbage collection, the memory of the object is actually recovered.

转载请注明原文地址:https://www.9cbs.com/read-21204.html

New Post(0)