I am curious about how clr knows particular object is not used by any other object and it is dead we know the basic of garbage collector but internally how clr find dead object.How clr knows the objects are in dead position.
Asked
Active
Viewed 878 times
-2
-
2Possible duplicate of [Understanding garbage collection in .NET](http://stackoverflow.com/questions/17130382/understanding-garbage-collection-in-net) – Samvel Petrosov May 22 '17 at 05:54
-
understand the garbage collection mechanism but how clr knows it is ready for garbage collection ? – Gagan Burde May 22 '17 at 06:47
-
Short answer: an object is dead when it is no longer reachable. Long answer: Objects that are no longer reachable are collected by a [generational GC](https://en.wikipedia.org/wiki/Tracing_garbage_collection#Generational_GC_.28ephemeral_GC.29). Read [this](https://msdn.microsoft.com/en-us/library/ms973837.aspx) and [this](https://msdn.microsoft.com/en-us/library/ee787088(v=vs.110).aspx) of how it is implemented within the CLR (and how an object being reachable is defined). – Ilian May 22 '17 at 06:53
-
2no research effort at all, the web is literally full of articles about clr gc – Vitaliy Terziev May 22 '17 at 07:09
-
1Google "garbage collector mark and sweep phase" for basic articles. – Hans Passant May 22 '17 at 07:16
1 Answers
1
https://msdn.microsoft.com/en-us/library/ee787088(v=vs.110).aspx#Anchor_4
A garbage collection has the following phases:
- A marking phase that finds and creates a list of all live objects.
- A relocating phase that updates the references to the objects that will be compacted.
- A compacting phase that reclaims the space occupied by the dead objects and compacts the surviving objects. The compacting phase moves objects that have survived a garbage collection toward the older end of the segment.
The garbage collector uses the following information to determine whether objects are live:
Stack roots. Stack variables provided by the just-in-time (JIT) compiler and stack walker.
Garbage collection handles. Handles that point to managed objects and that can be allocated by user code or by the common language runtime.
Static data. Static objects in application domains that could be referencing other objects. Each application domain keeps track of its static objects.

Vitaliy Terziev
- 6,429
- 3
- 17
- 25