What is garbage collection?
Garbage collection is taking memory that was referenced by a program but is no longer and freeing it so it can be used by the program again. Garbage collection automates memory management taking it out of the hands of the programmer. While this often makes programming more hassle free it does add some overhead to running the program. That is the CPU must spend time garbage collecting when usually it would spend the same time processing.
What are some garbage collection algorithms?
One older method of garbage collection was reference counting. For each object a count of how many references to that object is kept. If the count ever goes to 0 then the object is garbage collected and the count for all the objects it had references to is decremented. This method is simple reducing the amount of overhead, however objects can reference each other leading to cycles where the count will never be 0 and objects that have no reference from the program will never be garbage collected.
A more popular algorithm is called mark and sweep. In this algorithm the garbage collector starts at certain root nodes in the program and begins branching through all the references made by the root nodes and their children. When an object is referenced it is marked so. At the end of the traversal all objects that are not marked are known to be unreferenced and can be garbage collected. This algorithm has more overhead than reference counting but is more efficient in that it finds all collectable objects.
What are some other considerations one must consider when dealing with garbage collection?
Since garbage collection is in charge of freeing memory it must also keep memory unfragmented. This means it has to move objects around in memory to keep free memory sections contigous.
When does the garbage collection get called? Garbage collection can take a long time to run and the programmer does not know when it is scheduled. This means there can be random pauses in your program for random periods of time.
Nice choice of questions. In particular, the last one gets at what are some of the trickiest areas of garbage collection.
Links to this Page
Nicholas Beckmann last edited on 9 December 2005 at 11:56 am by r34h59.res.gatech.edu