Unfortunately, there is a lot of careless use of terminology related to garbage collection, which causes a lot of confusion. The “elimination” or “finalizer” does not actually destroy the object, but rather serves to delay the destruction of the object, which might otherwise have the right to destruction, while after it had the opportunity to put its affairs in order (that is, generally speaking, letting other things know that their services are no longer required).
It’s easiest to think of a “stop the world” garbage collector by following these steps to:
- Check all items that are new enough to be considered garbage.
- Visit every root of the garbage collector (that is, a thing that is essentially “living”), and if it has not yet been copied, copy it to a new heap, update the link to point to the new object, and all the elements to which it contains links (which will copy them if they were not copied). If someone visits an item in an old heap that has been copied, just update the link used to visit it.
- Inspect each item registered for finalization. If it has not already been copied, cancel it for final revision, but add a link to it in the list of objects that should be completed as quickly as possible.
- Elements of the immediate update list are considered "live", but since they have not yet been copied, visit each element in this list and, if it has not yet been copied, copy it to a new heap and view all the elements to which it has links.
- Drop the old heap, as no one else will refer to anything on it.
It is interesting to note that although some other garbage collection systems operate using pointers with double indirect access for links, the .net garbage collector (at least the usual “stop the world”) uses direct pointers. This slightly increases the workload of the collector, but it improves the efficiency of the code that controls the objects. Since most programs spend more time manipulating objects than collecting garbage, this is a clear victory.
supercat
source share