In terms of memory resource allocation,. Net can be divided into managed resources and unmanaged resources. For managed resources, the. NET Framework is responsible for garbage collection. For unmanaged resources, we need to manually release the resources. In general, we use the dispose () method in the idisposable interface to release resources, and the responsibility for calling the dispose () method is executed by the resource owner or object owner, that is, our programmers, the best way to call the dispose () method is to use using or try/finally statement blocks.
All types with unmanaged resources should implement the idisposable interface. In addition, as a safeguard measure, this type also creates a terminator to prevent us from forgetting to call the dispose () method, if we forget to call the dispose () method, the non-memory resources will be released when the Terminator is executed subsequently. In this way, the object remains in the memory for a long time, and the application cleans up resources slowly.
We can use the Using Method to forcibly call the dispose () method and view the following code.
Code
1 public void ExecuteCommand( string connString,
2 string commandString )
3 {
4 using ( SqlConnection myConnection = new
5 SqlConnection( connString ))
6 {
7 using ( SqlCommand mySqlCommand = new
8 SqlCommand( commandString,
9 myConnection ))
10 {
11 myConnection.Open();
12 mySqlCommand.ExecuteNonQuery();
13 }
14 }
15 }
In the above Code, sqlconnection and sqlcommand are two places where resources need to be released. In the variable declaration, using is used, and its function is the same as the following code.
Code
1 SqlConnection myConnection = null;
2
3 // Example Using clause:
4 using ( myConnection = new SqlConnection( connString ))
5 {
6 myConnection.Open();
7 }
8
9 // example Try / Catch block:
10 try {
11 myConnection = new SqlConnection( connString );
12 myConnection.Open();
13 }
14 finally {
15 myConnection.Dispose( );
16 }
NOTE: If we use the using statement for an object that does not implement the idisposable interface, the C # compiler will produce an error. Only when the idisposable interface is implemented for the compilation type, the Using statement can be compiled normally.
We can use as for type conversion to prevent compilation errors.
1 object obj = Factory.CreateResource( );
2 using ( obj as IDisposable )
3 Console.WriteLine( obj.ToString( ));
In the above Code, if OBJ does not implement the idisposable interface, it becomes using (null). This method is safe and will not produce mutation errors, but will not do anything.
When releasing resources of some objects, we will find that some types support both the dispose () method and the close () method. For example, the sqlconnection type, we can also call close () to release resources.
The difference between the dispose () method and the close () method: In addition to releasing resources, the dispose () method also notifies the Garbage Collector to terminate the object, it is implemented by calling GC. the suppressfinalize () method is implemented. The close () method generally does not process this way. Therefore, the object that calls the close () method will remain in the end queue. Therefore, the dispose () method should be called first.
The dispose () method does not delete an object from the memory. It only releases the object from unmanaged resources. This means that if the released object is still in use, some problems may occur, therefore, we should not release objects that are still referenced elsewhere in the program.