Memory Management in CLR

Source: Internet
Author: User

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.

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.