What is garbage collection, why do we do it, and how do we do it?
Definition: OOP Virtual machine's heap stores all objects created by running its OOP application, but objects are never freed explicitly. Garbage collection is the process, which AUTOMATICALLY recovers (frees) spaces that are no longer needed to provide more spaces.
1) Make programming more simpler (No needed to allocate the memory by programmer).
2) Make programming more reliable (About 40% of problems were memory related problems).
3) Make programming more safer (No pointer involved).
Q: Worldís first program language that used gargbage collection?
A: LISP in late 60ís.
Q: Any disadvantages?
A: It makes a program running slower. However, this is not really a big issue in todayís world since most of computers today run on faster processor.
Another disadvantage to consider is that you cannot guarantee that an operation will happen in a certain amount of time. A full garbage collect can take a significant amount of time. Thus, GC languages are generally bad for real-time applications
How? (Three approaches were introduced in class) 1. Reference counting: It distinguishes live objects from garbage objects by keeping a count for each object on the heap. The count keeps track of the number of references to that object
PRO: There is no interrupt while itís doing garbage collection. Meaning, other things could be running while itís garbage collecting.
CON: Since this requires a keeping track of references, what if two object are referring to each other (cycle). These objects will never have a reference count of zero.
Reference counting is currently out of favor because it created so much problems of referring to one another.
2. Tracing (Most VMs that we encounter today, use this approach)
Tracing garbage collectors trace out the graph of object references starting with the root nodes. Objects that are encountered during the trace are marked in some way. Marking is generally done by either setting flags in the objects themselves or by setting flags in a separate bitmap. After the trace is complete, unmarked objects are known to be unreachable and can be garbage collected.
The heap is divided into two or more sub-heaps, each of which serves one "generation" of objects. The youngest generation is garbage collected most often. As most objects are short-lived, only a small percentage of young objects are likely to survive their first collection. Once an object has survived a few garbage collections as a member of the youngest generation, the object is promoted to the next generation: it is moved to another sub-heap. Each progressively older generation is garbage collected less often than the next younger generation. As objects "mature" (survive multiple garbage collections) in their current generation, they are moved to the next older generation.