1. The class is nothing more than a blueprint that describes how an instance of this type will look and feel in memory.
2. When Garbage collector destroy a object in memory? The short (i.e., incomplete) answer is that the garbage collector removes an object from the heap when it is unreachable by any part of your code base.
3. understand that the managed heap is more than just a random chunk of memory accessed by the
CLR. The .NET garbage collector is quite a tidy housekeeper of the heap, given that it will compact empty blocks of memory (when necessary) for purposes of optimization. To aid in this endeavor, the managed heap maintains a pointer (commonly referred to as the next object pointer or new object pointer) that identifies exactly where the next object will be located.
4. When a collection does take place, the garbage collector temporarily suspends all active threads within the current process to ensure that the application does not access the heap during the collection process.
5.
• Generation 0: Identifies a newly allocated object that has never been marked for collection
• Generation 1: Identifies an object that has survived a garbage collection (i.e., it was marked for collection, but was not removed due to the fact that the sufficient heap space was acquired)
• Generation 2: Identifies an object that has survived more than one sweep of the garbage collector
6. When you manually force a garbage collection, you should always make a call to GC.
WaitForPendingFinalizers().With this approach, you can rest assured that all finalizable objects have had a chance to perform any necessary cleanup before your program continues forward.
7. The only reason to override Finalize() is if your C# class is making use of unmanaged resources via PInvoke or complex COM interoperability tasks (typically via various members defined by the System.Runtime. InteropServices.Marshal type).
8.we have seen two different approaches to construct a class that cleans up internal
unmanaged resources. On the one hand, we could override System.Object.Finalize(). Using this
technique, we have the peace of mind that comes with knowing the object cleans itself up when
garbage collected (whenever that may be) without the need for user interaction. On the other hand,
we could implement IDisposable to provide a way for the object user to clean up the object as soon
as it is finished. However, if the caller forgets to call Dispose(), the unmanaged resources may be
held in memory indefinitely.
9. The current implementation of MyResourceWrapper does work fairly well; however, we are left with a few minor drawbacks. First, the Finalize() and Dispose() methods each have to clean up the same
unmanaged resources. This could result in duplicate code, which can easily become a nightmare to
maintain. Ideally, you would define a private helper function that is called by either method. Next, you would like to make sure that the Finalize() method does not attempt to dispose of any managed objects, while the Dispose() method should do so. Finally, you would also like to make sure that the object user can safely call Dispose() multiple times without error. Currently, our Dispose() method has no such safeguards. To address these design issues, Microsoft has defined a formal, prim-and-proper disposal pattern that strikes a balance between robustness, maintainability, and performance. Here is the final (and annotated) version of MyResourceWrapper, which makes use of this official pattern:
public class MyResourceWrapper : IDisposable { // Used to determine if Dispose() // has already been called. private bool disposed = false; public void Dispose() { // Call our helper method. // Specifying "true" signifies that // the object user triggered the cleanup. CleanUp(true); // Now suppress finalization. GC.SuppressFinalize(this); } private void CleanUp(bool disposing) { // Be sure we have not already been disposed! if (!this.disposed) { // If disposing equals true, dispose all // managed resources. if (disposing) { // Dispose managed resources. } // Clean up unmanaged resources here. } disposed = true; } ~MyResourceWrapper() { // Call our helper method. // Specifying "false" signifies that // the GC triggered the cleanup. CleanUp(false); } }