Live objects and dead objects - the basics
suggest changeRule of thumb: when garbage collection occurs, “live objects” are those still in use, while “dead objects” are those no longer used (any variable or field referencing them, if any, has gone out of scope before the collection occurs).
In the following example (for convenience, FinalizableObject1 and FinalizableObject2 are subclasses of FinalizableObject from the example above and thus inherit the initialization / finalization message behavior):
var obj1 = new FinalizableObject1(); // Finalizable1 instance allocated here var obj2 = new FinalizableObject2(); // Finalizable2 instance allocated here obj1 = null; // No more references to the Finalizable1 instance GC.Collect();
The output will be:
<namespace>.FinalizableObject1 initialized <namespace>.FinalizableObject2 initialized <namespace>.FinalizableObject1 finalized
At the time when the Garbage Collector is invoked, FinalizableObject1 is a dead object and gets finalized, while FinalizableObject2 is a live object and it is kept on the managed heap.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents