SqlConnection ()) {} method to create connection, because when the. Net Framwork automatically executes the Connection.dispose () method, so you can ensure that the connetion is closed in a timely manner.1) so timely call. Dispose () is really that heavy either, if an object goes beyond the living space, is it automatically cleaned up in. NET by the GC (garbage collector)?This problem is actually due to
Valid tive C # item18: implement the standard dispose Pattern
Releasing unmanaged resources is an important part. For custom types, if resources need to be released, we should use the standard mode of. NET Framework to release unmanaged resources. In standard mode, the idisposable interface is used to explicitly release resources, and the finalizer is used to release resources when users forget to release resources. Whether the object Terminator shoul
This article is the third of the GC expert series. In the first understanding of Java garbage Collection, we learned several different GC algorithm processing processes, the way GC works, the difference between the new generation and the old age. So, you should have known about the 5 GC types in JDK 7, and the performa
Http://www.cnblogs.com/eddyshn/archive/2009/08/19/1549961.htmlThere are actually two functions for freeing resources in. NET objects:Dispose and Finalize. The purpose of finalize is to release unmanaged Resources , while Dispose is used to free all resources, both managed and unmanaged.In this pattern, the void Dispose (bool disposing) function uses a disposing parameter to distinguish whether the current i
=Marshal.allochglobal (100); //Demo Creating a managed resource PrivateAnotherresource Managedresource= NewAnotherresource (); Private boolDisposed= False; /// ///Implementing the Dispose method in IDisposable /// Public voidDispose (){ //Must be TrueDispose (True); //Notifies the garbage collection mechanism not to call finalizers (destructors)Gc. SuppressFinalize (This);} /// ///Not necessary, provide
The destructor in C # actually overwrites the virtual method in system. object.Finalize
The three most common methods are as follows:
1. destructor (called by GC, not sure when to call it)
2. inherit the idisposable interface to implement the dispose method. (You can call it manually. For example, the connection to the database, sqlconnection. Dispose (),
will analyze the structure and execute garbage collection.F. If an object has a Finalize method, the garbage collector will automatically call this method before it recovers its memory.G. In this way, we can clean up those (unmanaged resources ).
From this point of view, the mechanism provided by the garbage collector is to better improve the. net automatic memory management function, so that we can also participate in the garbage collection.
10. Let's take a look at what
(bool disposing){So this method is called directly by the client, so both managed and unmanaged resources can be freedif (disposing){Releasing managed resourcesOthermanagedobject.dispose ();}Freeing unmanaged ResourcesDounmanagedobjectdispose (); This method is called directly by the customer, telling the garbage collector to purge itself from the finalization queue, thereby preventing the garbage collector from invoking the Finalize method.if (disposing)Gc
is as follows.
2. Generational Algorithms. Net divides the memory hosting heap into three generations, Gen 0, Gen 1, Gen 2. If Gen 0 heap memory reaches the threshold, the 0 generation GC is triggered, after 0 generations of GC, the surviving objects in Gen 0 enter Gen1. If the memory of Gen 1 reaches the threshold value, perform 1 generation GC, one-generation
Anotherresource ();private bool disposed = false;Implementing the Dispose method in IDisposablepublic void Dispose (){Must be TrueDispose (TRUE);Notifies the garbage collection mechanism to no longer invoke finalizers (destructors)Gc. SuppressFinalize (this);}is not necessary, and provides a close method only to conform to the specifications of other languages (
PrivateAnotherresource Managedresource =NewAnotherresource (); Privatebool disposed=false; /// ///implementing the Dispose method in IDisposable///publicvoid Dispose () {//must be TrueDispose (true); //notifies the garbage collection mechanism not to call finalizers (destructors)Gc. SuppressFinalize ( This); } /// ///not necessary, provide a close me
explanation of team manager Richard Wong and I hope it will help you. Thank you, Richard!
The Demo code is as follows:
1
Using
System;
2
3
Namespace
Consoleapplication
4
{
5
Abstract
Class
Mystream: idisposable
6
{
7
Public
Mystream ()
8
{
9
M_unmanagedresource
=
Marshal. alloccotaskmem (
100
);
10
M_bitmap
=
New
Bitmap (
50
,
50
);
11
}
12
13
# Region
Idisposable members
14
Void
I
);
//
Demonstrate creating a Managed Resource
Private
Anotherresource managedresource
=
New
Anotherresource ();
Private
Bool
Disposed
=
False
; /// /// Implement the dispose method in idisposable /// Public Void Dispose (){ // Must be true Dispose ( True ); // Notifies the garbage collection mechanism not to c
(); Dispose () to call another virtual Dispose (bool) function. Therefore, if you inherit from this class, you must implement the Dispose (bool) method.
When you call Close (), you will call the overloaded Dispose (bool) method to release the resource.The following is a standard implementation method that inherits the
block, when leaving the using program block, the CLR automatically calls the Dipose method of the object created by the type.One might ask why some types need to provide a close method, since the release of the resource can be done in the way of the Dispose method. The difference, or the Convention, is that if you look closely at these types: they basically only expose the Close method, they all implement the IDisposable, but both hide the
release of unmanaged resources must implement this interface. This interface has only one method, dispose (), but there is a relative guidelines indicating how to implement this interface. Here I will explain it to you. Classes that implement the idisposable interface need to have the following structure:
[C #] public class base: idisposable {public void dispose () {This.
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.