#732 – Destruction vs. Collection
December 10, 2012 Leave a comment
Automatic memory management in C# means that the memory for a heap-based object is automatically released when there is no longer any user-written code that references the object. This is done by the garbage collector.
An object goes through two distinct phases when it is no longer referenced–destruction and collection.
When an object is no longer referenced by any user-written code, other than code in a destructor, the object becomes eligible for destruction.
Once the object is eligible for destruction, it may be destructed at some point in the future, i.e. its destructor is called.
After the object is destructed and it is no longer being referenced by any code, including code in a destructor, the object becomes eligible for collection.
Once the object is eligible for collection, the garbage collector may at some point in the future release memory for the object.