Garbage collection (Basics)

A garbage collector is a hidden background tool that cleans unused application memory automatically. It’s necessary for languages which aren’t providing any way to explicitly deleting created reference objects on the heap, like Java and C#.


Strategies

1. Reference counting
For every reference object it keeps a reference counter and if that hits zero that object gets destroyed.

Adaptation: In C++ smart pointers implementing this strategy, but they have to be used explicitly and they do the reference counting by them self.

Disadvantage: Cyclic references can’t be resolved automatically.

2. Tracing
It keeps a graph of the reference objects and if there is no way to a ref. object anymore it destroys it.

Algorithms:
– Mark-and-Sweep (Fragments the heap)
– Stop-and-Copy (Consumes much heap)
– Mark-and-Compact (Defragmentation takes time)

Improvement: Implementation of the concept of generations.