Framework: Understanding the Garbage Collector
This sample shows some how to control how the Garbage Collector
operates. It demonstrates when the garbage collector calls any
Finalize methods on created objects, as well as how to supress
finalization.
Featured Highlights:
This tutorial demonstrates how to use the Garbage Collector (GC). In particular,
it discusses:
Using the GC.Collect() method to force the garbage collector to run.
Improving garbage collector performance on certain objects by using
the GC.SuppressFinalize() method.
How to mark an object for garbage collection (setting it equal to
Nothing).
Requirements:
Microsoft Visual Studio.NET Professional or greater.
Windows XP or Windows 2000
Running the Sample:
To run the sample:
-
Doubleclick the How-To Intro to the Garbage Collector.sln
file.
-
Click the buttons on the form to test the functionality.
-
Click the Create Objects
button to create a hierarchy of objects.
-
Select one of the objects in the list box, and use the Kill Selected Object
or Dispose Selected Object
button to kill or dispose of the object.
-
Press the Run GC
button to see the effect of forcing the garbage collector to run.
-
Notice in the Activity Log that when Dispose is called with
the Suppress Finalize in Dispose checkbox checked that
the Finalize method is not fired, and does not appear in the output.
-
Examine the code in the sample to understand how the code works.
-
Close the running application, and note how any unfinalized objects are
finalized as the form is closed.
See Also:
System.GC
Class,
GC.Collect Method,
GC.SuppressFinalize Method,
Programming For Garbage Collection