[Zz] managing heap (bottom)

Source: Internet
Author: User

Objects can be cleared in two ways. The first method isIdisposableInterfaceDisposeMethod. This method is called by the client code when the object ends explicitly.Internaldispose (true). In this case, all objects are cleared. If the Destructor is calledInternaldispose (false)Called. Only external resources are released. If we have already terminated the operation, our own objects may have been released, and subsequent references to them may cause exceptions.

PairGC. suppressfinalizeWill prevent the spam collector from placing the object in the termination queue. This can reduce the memory consumption caused by sorting objects during a GC process and improve the performance because the termination operation is not called.

For C #Optimization

 

ThereforeIdisposable. Dispose ()To release resources is a good way, it not only reduces some memory requirements for operations on the hosting stack, but also reduces the number of objects that must be terminated. However, it is troublesome to use, especially when multiple temporary objects are created. To benefit from the idisposable interface, the C # client program should write code like the following:

Overduebooklocator booklocator = NULL;

Try

{

Booklocator = new overduebooklocator ();

// Use booklocator here

Book = booklocator. Find ("Eiffel, the language ");

.

.

.

}

Finally

{

If (booklocator! = NULL)

{

Idisposable disp = booklocator as idisposable;

Disp. Dispose ();

}

}

FinallyThe code in is used for proper cleaning when an exception occurs. To enable the C # client program to use the dispose mode in a simple and effective manner, beta2 introducesUsingExpression.UsingThe expression allows you to simplify your code, so the above Code can be written:

Using (booklocator = new overduebooklocator ())

{

// Use booklocator here

Book = booklocator. Find ("Eiffel, the language ");

}

You should useUsingExpression. It ensures thatIdisposableAn appropriate call to an interface, even when an exception occurs.

UseSystem. GCClass

System. GCClass is used to access the garbage collection mechanism exposed by. NET Framework. This class contains the following useful methods:

GC. suppressfinalizeThis method has been described earlier and can suppress the termination operation. If you have released the external resources of an object, call this method to suppress the execution of the termination operation on this object.

GC. CollectIt has two versions. Versions without parameters are recycled on all the generations of the managed heap. The other version has an integer parameter that specifies the generation of the recycle operation. You will rarely call this method because the Garbage Collector will automatically call it as needed.

GC. getgenerationReturns the generation of the object passed in as a parameter. This method plays a significant role in debugging and tracking for performance reasons, but has limited role in most applications.

GC. gettotalmemoryReturns the total allocated memory in the heap. This number is not accurate because of the way the managed heap works, but if you use true as the parameter, you will still get a close approximate value. This method will be recycled once before calculation.

 

The following is an example of using these methods:

/// <Summary>

/// Displays current GC Information

/// </Summary>

/// <Param name = "Generation"> the generation to collect </param>

/// <Param name = "waitforgc"> run GC before calculating usage? </Param>

Public void collectandaudit (INT generation, bool waitforgc)

{

Int mygeneration = GC. getgeneration (this );

Long totalmemory = GC. gettotalmemory (waitforgc );

Console. writeline ("I am in generation {0}.", mygeneration );

Console. writeline ("memory before collection {0}.", totalmemory );

GC. Collect (generation );

Console. writeline ("memory after collection {0}.", totalmemory );

}

Author of this Article

 

Mickey Williams is one of the founders of codev technologies. Codev technologies is an organization engaged in consulting and tools provided by a Windows program developer. He is also a principal member of. Net experts (http://www.codeguru.com/columns/DotNet/www.dotnetexperts.com), where he teaches. NET Framework courses. He often gave speeches at seminars in the United States and Europe and has written eight books on Windows programming. He is currently being invited by Microsoft press to write "Microsoft Visual C #". You can find him in mw@codevtech.com.

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.