Every one knows - Garbage Collector cleans the memory from unused objects.
My question is, what is the parameters to define whether this object is unused or not?
The two major forms of garbage collection are Mark & Sweep, or reference counting.
Most modern day languages uses a combination or a variation of several methods. This will cover the general concept of each scheme.
Additionally, escape analysis is used to "reduce" the amount of "garbage".
This works by having all object, with a single boolean "marked" parameter.
This works on the concept of counting the amount of with an integer "parent references" parameter and deleting the object, when its count reaches zero.
Is an additional method used to "reduce" the amount of garbage, by analysing your executing code. For temporary variables, that will never be "persistent" after a function execution. These objects/variables are then immediately cleared after your function execution. Avoiding the need of adding an object to the garbage collection system (in which will have an overhead). This step is critical in ensuring the performance of many modern day languages.
Detailed advantages / disadvantages of Mark & Sweep / Reference Counting can be found on wiki
GC.KeepAliveor the C#fixedstatement. Note that both don't prevent the actual collection from happening, but just alter certain behaviors (follow the links to find out more). - Christian.K