If the operating enviroment has the responsibility to re-claim un-used objects than some kind method to do this is required. There seem to be two different ways to do this.
- MarkAndSweep. A two phase process, in phase one, objects are traversed by reachability from a root object. If the object is reachable, then the object is marked. In the second phase, all unmarked objects are discarded. This can be a slow process.
- ReferenceCounting. Every object maintains a count of how many objects hold references. During a de-reference, if the count drops to 0, the object is discarded. In addition to the extra memory overhead, cycles might exist where two or more objects hold references to each other preventing collection. The advantage of this method is that an object can be discarded immediately as part of the de-referencing process.
In practice, a combination of the two above methods might be used.
Usually garbage collection runs as a background process with a way
to explicitly invoke the garbage collector.
This is related to VirtualMemory. When free memory becomes scarce, the operating system has two choices: (a) Run garbage collection, or (b) Swap unused pages to a persistent storage medium.
See GarbageCollectionLinks for more information.
Content of these pages are owned and copyrighted by the poster.
|
Hosted by:
|
|