Cleaning Up Unmanaged Resources
What you should NOT do in cleaning up unmanaged resoures!
- You should prevent users of your application from calling an object's Finalize method directly by limiting its scope to protected.
- You are strongly discouraged from calling a Finalize method for a class other than your base class directly from your application's code.
How to properly clean up unmanaged resources?
@ To properly dispose of unmanaged resources, it is recommended that you implement a public Dispose or Close method that executes the necessary cleanup code for the object.
@ The IDisposable interface provides the Dispose method for resource classes that implement the interface. Because it is public, users of your application can call the Dispose method directly to free memory used by unmanaged resources.
@ When you properly implement a Dispose method, the Finalize method becomes a safeguard to clean up resources in the event that the Dispose method is not called.
Why do we need IDisposable?
- The garbage collector automatically releases the memory allocated to a managed object when that object is no longer used, however, it is not possible to predict when garbage collection will occur.
- Furthermore, the garbage collector has no knowledge of unmanaged resources such as window handles, or open files and streams.
@ Thus, we should use the Dispose method of this interface to explicitly release unmanaged resources in conjunction with the garbage collector.
@ The consumer of an object can call this method when the object is no longer needed.
@ When calling a class that implements the IDisposable interface, use the try-finally pattern to make sure that unmanaged resources are disposed of even if an exception interrupts your application.
@ Destructors are used to destruct instances of classes.
Using Destructors to Release Resources
@ In general, C# does not require as much memory management as is needed when developing with a language that does not target a runtime with garbage collection. This is because the .NET Framework garbage collector implicitly manages the allocation and release of memory for your objects.
When your application encapsulates unmanaged resources such as windows, files, and network connections, you should use destructors to free those resources. When the object is eligible for destruction, the garbage collector runs the object's Finalize method.
@ A class can only have one destructor.
@ Destructors cannot be inherited or overloaded.
@ Destructors cannot be called. They are invoked automatically.
@ A destructor does not take modifiers or have parameters.
@ Destructors cannot be defined in structs. They are only used with classes.
@ The programmer has no control over when the destructor is called because this is determined by the garbage collector.
The garbage collector checks for objects that are no longer being used by the application. If it considers an object eligible for destruction, it calls the destructor (if any) and reclaims the memory used to store the object.
Destructors are also called when the program exits.
e Implementing a Dispose Method