Professional Documents
Culture Documents
04 InternalsOfGarbageCollection (GC)
04 InternalsOfGarbageCollection (GC)
04 InternalsOfGarbageCollection (GC)
John McCarthy
(September 4, 1927 – October 23, 2011).
Professor of computer science,
helped to invent the field of
Artificial Intelligence (AI).
Terminology
“Root Objects” are the objects from where the references
to other objects start. Objects in blue are “Reachable
Objects” (Active References).
These objects are the candidates for entry point for GC
and can never go unreachable. The arrow shows the references
between them.
“Root Set” – refers to the collection of root objects.
Mark-and-Sweep collection
Mark-and-don’t-Sweep
• Travarse the entire memory graph starting from root and mark
objects that are visited.
MARK
• Remove all objects that are not visited. It starts scanning objects
from 1st address in the heap.
• If mark flag is not set => Reclaim;
Sweep • If mark flag is set => Clear the flag.
Strong Reference
• The garbage collector can reclaim only objects that have no references. An object that is
reachable is said to have “Strong Reference” which cannot be collected by the garbage
collector.
Weak Reference
• An object is eligible for garbage collection if it does not contain any strong references,
irrespective of the number of weak references it contains. Also known as the target.
• The primary advantage of maintaining weak references to an object is that it allows the
garbage collector to collect or reclaim memory of the object if it runs out of memory in
the managed heap.
• There are 2 types of weak reference…
3
GC Execution – When / How
The !Marked() check will ensure that each Object is marked only
once and avoid endless recursive loop.
Sweep algorithm
Sweep starts at the beginning of the Heap to the end of the Heap.
Compaction
Done to…
Decrease fragmentation
Increase allocation speed
Increase locality of reference
known as “infant
mortality” or the
“generational
hypothesis”
Generational GC
To avoid this Higher Gen Ref to Lower Gen issue; each time the object
assignment is made; the CLR checks if this scenario exists. If yes then it
maintains a table of such references called ‘Card Table’ which GC will
use. This will mark the blue object as new root.
Finalization
Finalization
When GC runs, and the Object with Finalizer is marked for Sweep; the
Sweep() will check for the reference in the FAL and move the reference to
Finalization Ready List (FRL).
Finalization
Soon after the GC Run 1 is done, the Finalizer thread is kicked off.
This thread will look in FRL and fires the Finalizer() of the Object
and remove the reference in FRL.
Finalization
Finalization
When GC Run 2 kick off – it checks references in FAL; when not found it will
remove the object. Thus objects with finalizer is guaranteed that they will get
promoted to higher generation; hence it is very expensive.
Finalizer is not Guaranteed to run: Also, when the Finalizer thread calls the
respective Object.Finalizer() and if one of the Finalizer() breaks/hangs; the
entire thread will be aborted; resulting in the Object.Finalizer() for remaining
objects never gets called.
.Net Framework (FW) uses Generational GC. The table below shows the GC operation mode
supported in each CLR version.
1.1 1.1 Concurrent Visual Studio .NET 2003 Windows Server 2003