[C #] C # Study Notes-garbage collection mechanism

Source: Internet
Author: User

C # Study Notes-garbage collection mechanism

Luo chaohui (http://www.cnblogs.com/kesalin)

C # And. Net advancedProgramDesign Reading Notes

 

1. C # garbage collection mechanism

C #'s garbage collection mechanism is not based on reference counting, but on whether objects can be reached. The operation process of this mechanism is: CLR will create an object graph, representing every object that can be reached on the stack. If a garbage collection process occurs, if an object does not have root (no other object depends on it) on the graph, the object is an inaccessible object and is marked as spam and ended, clear from memory. Microsoft recyclingAlgorithmThe object generation is used to terminate the auxiliary thread of an object and the managed heap dedicated to carrying large objects for optimization to improve the efficiency of the garbage collection mechanism.

2. End the process

Finalize () is used to ensure that. Net objects can be cleared during garbage collection Unless managed resources are managed. If you create a type that does not use an unmanaged entity, the termination is useless. In fact, it is best to avoid providing finalize () during design to improve efficiency, because the end takes time. When an object is allocated on the managed stack, the runtime automatically determines whether the object provides a custom finalize () party. If so, the object is markedTerminableAnd a pointer pointing to this object is saved toEnd queue. An ending queue is a table maintained by the garbage collector. It points to each object that must be terminated before being deleted from the stack.

When the Garbage Collector determines that an object needs to be released from memory, it checks every item in the end queue and copies the object from the stack to another managed structure called the final reachable table. In this case, another thread is generated during the next garbage collection, and the finalize () method is called for each object in the reachable table. Therefore, to truly end an object, at least two garbage collections are required.

All in all, although the end of an object can ensure that the object can clear unmanaged resources, it is still uncertain in nature and will become quite slow due to additional background processing. To release unmanaged resources as quickly as possible, we introduced the idisposable interface, which defines a method named dispose (). This method assumes that when the object user no longer uses this object, call dispose () manually before this object reference leaves the scope (). In this way, the object can be cleared of any necessary unmanaged resources, without the performance loss caused by the end of the object on the end queue, and does not have to wait until the end logic of the garbage collection trigger class.

Finalize () is only applicable to the class type, while the idisposable interface is applicable to both the structure and the class type. Note: if an object supports the idisposable interface, always call dispose () for any directly created object. If the class designer chooses to implement this interface, the class must be manually cleaned up. Note: many types in the base class library implement this interface and provide aliases for the dispose () method, such as system. Io. filesteam class close (). We can always call dispose () correctly regardless of the alias.

3. The Using Keyword can be reused to simplify the compilation of manually calling the dispose () method in try/finally blocks. The Using syntax ensures that when a. Net-type object that supports the idisposable interface is used to exit the using block, this object will automatically call the dispose () method.

4. The final type is to overwrite the system. object. the finalize () virtual method (in the destructor, write the clear Code ) to clear the classes that are hosted unless garbage collection is performed. The detachable object is a class or structure that implements the idisposable interface. The object user will call it after the idisposable interface is implemented. We can combine these two methods. If the Object User calls dispose (), it can call GC. suppressfinalize () notifies the Garbage Collector to skip the termination process. If the Object User forgets to call dispose (), the object will eventually be terminated and has the opportunity to release internal resources. The internal unmanaged resources of the object are always released in one way.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.