In C ++, the memory allocated on the stack must be manually recycled. That is, the new and delete operators must be used in pairs. In this wayProgramDevelopers provide powerful and flexible control capabilities, allowing programmers to control when to destroy objects and reclaim memory. This kind of control capability makes C ++ programmers feel that they have a strong ability to control the program. However, this manual recovery mechanism is very difficult to control. Even experienced C ++ programmers may also avoid omissions, resulting in Memory leakage in the program, leading to slower and slower system operation, then the system crashes. This is also the most criticized part of the manual recovery mechanism.
Therefore, many so-called modern programming languages, such as Java and C #, provide an automatic garbage collection mechanism. After the object is no longer referenced, the system automatically destroys the object and recycles the memory. In managed C ++, you can use language extensions to automatically recycle garbage.
Automatic garbage collection does not require the intervention of programmers. It automatically recycles discarded objects and releases memory. It effectively avoids Memory leakage, but it also brings about new problems: 1. The collection time of discarded objects is uncertain, that is, you cannot determine when the system will perform garbage collection. Each Garbage Collector follows a certainAlgorithmGarbage collection is executed, but it is uncertain when it is executed and what objects are recycled each time. 2. It causes the programmer to lose part of the control capability, and the performance will also suffer some losses during system operation.
In C #, objects are destroyed and recycled in a flexible way. It allows immediate destruction of objects, memory collection, and automatic collection through the garbage collector. This can be done because C # provides two ways to destroy objects:
1. The class supports the idisposable interface. After each object is used, the class immediately uses the manual dispose () method, or the using block implicitly calls the dispose () method, the object is destroyed immediately. In the dispose () method, the overload that calls the dispose () method with a Boolean parameter is written manually in this overload function.CodeRelease all the hosted and unmanaged resources opened by the object.
2. The Garbage Collector automatically recycles the object. When the Garbage Collector destroys an object, it calls the class destructor to release the unmanaged resources opened by the object, destroy the object, and recycle the memory. Because the Destructor is called only when the garbage collector is used for collection, you only need to release the unmanaged Resources in the destructor, will be automatically destroyed by the garbage collector. The release of an unmanaged resource by the Destructor is also implemented by calling an overload with a Boolean parameter in the dispose () method.
The preceding description shows that in the C # class, the operation code that actually releases the resource is in the overload of the dispose () method with a Boolean parameter. Both dispose () and destructor release resources by calling this overload. This method ensures that all Code related to resource release is concentrated in one function for easy maintenance.
This implementation that allows immediate destruction of objects and automatic collection by the garbage collector fully reflects the flexibility of C #. I think it is one of the powerful functions of C.