Garbage Collection

   

Although I've talked a lot about objects (you can't talk about anything .NET related without talking about objects), I've avoided discussing the underlying technical details of how .NET creates, manages , and destroys objects. Although you don't need to know the complex minutiae of how .NET works with objects, you do need to understand a few details of how objects are destroyed .

graphics/newterm.gif

As I discussed in previous hours, setting an object variable to null or letting it go out of scope destroys the object. However, as I mentioned in Hour 17, "Designing Objects Using Classes," this isn't the whole story. The .NET platform uses a garbage collector for destroying objects. The specific type of garbage collection implemented by .NET is called reference-tracing garbage collection. Essentially, the garbage collector monitors the resources used by a program, and when resources reach a defined threshold, the garbage collector proceeds to look for unused objects. When the garbage collector finds an unused object, it destroys it, freeing all the memory and resources the object was using.

An important thing to remember about garbage collection is that releasing an object by setting it to null or letting an object variable go out of scope doesn't mean the object will be destroyed immediately. The object won't be destroyed until the garbage collector is triggered to go looking for unused objects.


   
Top


Sams Teach Yourself C# in 24 Hours
Sams Teach Yourself Visual Basic 2010 in 24 Hours Complete Starter Kit (Sams Teach Yourself -- Hours)
ISBN: 0672331136
EAN: 2147483647
Year: 2002
Pages: 253
Authors: James Foxall

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net