Here, the object can be cleared in two ways. The first way is the Dispose method of the IDisposable interface. This method is called by customer code at the end of the object explicitly, which calls INTERDISPOSE (TRUE). All objects are cleared in this case. If the destructor is called, INTERNALDISPOSE (FALSE) is called, and only external resources will be released. If we have already performed the end operation, then our own object may have been released, and thereafter it will cause an exception to their references.
The call to GC.SuppressFinalize prevents the garbage collector from placing the object in the termination queue. This can reduce memory consumption caused by the object during a GC, and the performance is improved due to termination operations.
Optimization of C #
So use iDisposable.dispose () to release resources is a good way, but can not only reduce some memory requirements on the hosted pile, but also reduce the number of objects that must perform termination operations. But it is more troublesome, especially when there are multiple temporary objects being created. In order to benefit from the IDisposable interface, the C # client should write code like this:
OverduebookLocator Booklocator = NULL;
Try
{
Booklocator = new overduebooklocator ();
// Use booklocator here
Book book = booklocator.find ("Eiffel, The Language");
.
.
.
}
Finally
{
IF (BookLocator! = NULL)
{
Idisposable Disp = Booklocator as IDisposable;
Disp.dispose ();
}
}
The code in Finally is used to make the appropriate cleanup work in an abnormality. For the C # client program, Beta2 introduces the use of uses. Using Expressions Allow you to simplify your code, so the above code can be written:
Using (Booklocator = New overduebookLocator ())
{
// Use booklocator here
Book book = booklocator.find ("Eiffel, The Language");
}
You should use the USING expression whenever you allocate the type of life with a clear definition. It guarantees an appropriate call to the IDisposable interface, even when there is an abnormality.
Use system.gc class
System.gc class is used to access the garbage collection mechanism exposed by the .NET Framework. This class contains some useful methods:
● GC.SuppressFinalize This method has been described above, which can suppress termination operations. If you have released an external resource belonging to an object, call this method to suppress the execution of the termination operation of this object.
● gc.collect has two versions. The recycled action is performed on all the generation of the hosted stack without parameters. Another version comes with an integer parameter, which indicates the geneAration to be recycled. You will quite call this method because the garbage collector will automatically call it when needed.
● gc.getGeneration returns the generation of the object that is incorporated into the parameter. This method is active in debugging and tracking due to performance, but is limited in most applications.
● gc.gettotalmemory returns the total amount of memory already assigned in the heap. This number is not accurate because of the hosted stack, but if you use true as a parameter, it will still get a relatively close approximation. This method will execute a recycling operation before calculating. Here is an example of using these methods:
///
/// Displays current GC Information
/// summary>
/// The generation to collect param>
/// Run GC Before Calculating usage? param>
Public Void Collectandaudit (Int Generation, Bool WaitForgc)
{
INT myGeneration = gc.get generation (this);
Long TotalMemory = gc.gettotalmemory (waitforg);
Console.writeline ("i am in generation {0}.", Mygeneration;
Console.writeline ("Memory Before Collection {0}.", TOTALMEMORY);
Gc.collect (generation);
Console.writeline ("MemoryAfter Collection {0}.", TOTALMEMORY);
}
About this article
MICKEY WILLIAMS
Yes
Codev Technologies
One of the founders.
Codev Technologies
Is a family
Windows
Program developers provide institutions for consulting and tools. He is also
.NET Experts (http://www.codeguru.com/columns/dotnet/www.dotnetexperts.com)
The main member, he is teaching here
.NET Framework
Courses. He often delivered a speech at some of the US and Europe's research, and eight related
Windows
Programming works. He is currently being invited by Microsoft Publishing House. "
Microsoft Visual C #
"You can
MW@codevtech.com
find him.