In C ++ProgramThe biggest headache for Members is the allocation and management of memory. In. net, it is much easier .. Net references the garbage collection (GC) function, which replaces the work of programmers to clear useless objects. Although in most cases, we do not need to pay attention to memory collection, but if we can add some memory management work in the program in a timely manner, we can make the program more optimized.
Since CLR (runtime in the common language) can know all object references in the system, GC can obtain information about whether the object is referenced at runtime. If an object is no longer referenced, it is automatically recycled by GC.
However, GC recovery is performed only when the specified resources are insufficient. If we want to control it by ourselves, we can also explicitly instruct GC to work. The method is:
System. gc. Collect ();
GC first identifies whether an object is referenced and marks its features. Only objects not referenced are recycled. To avoid heap fragmentation, GC will re-allocate the memory after the object is recycled and relocate the unrecycled object. This will inevitably lead to a reduction in the system running performance during GC recovery.
Timely manual intervention in memory allocation is a good choice. We know that in C ++, the corresponding destructor exists in the created class to delete the memory. The same method can be used in C. When a class object is instantiated and deleted, its destructor is automatically called. CLR provides the object finalization mechanism and introduces the Finalize method. However, in C #, the Finalize method cannot be directly implemented. Instead, the finalize () method of the base class is called in the destructor.
GC's collection mechanism is asynchronous. We can use the dispose () method provided by CLR to delete each object. The dispose () method is provided by the idiposable interface. Therefore, for the class object to be instantiated to implement the dispose operation, the class must implement this interface and provide the dispose () method.
Public class garbage: idisposable // implement this interface
{
Public void dispose () // provides the dispose () method;
{
GC. suppressfinalize (this); // reclaim this object;
}
~ Garbage () // destructor;
{
Dispose ();
}
}
However, a better solution is to use the using statement. Put the instance of the object into using. Once using is finished, the system automatically determines the object.
Using (garbage G = new garbage ())
{
// Perform the operation;
}
However, it should be noted that the instance object in the using statement must also implement the idisposable interface and the dispose () method.
In addition, because icomponent extends idisposable, The icomponent type is always the idisposable type. Therefore, the component type we developed can be used in using or the dispose () method. Therefore, instances of components provided by the system, such as dataset and datatable, can also clear objects in this way.
Refer to msdn and. Net references.