This is very interesting question. Let me explain you Obj A -> Obj B ( object A refers to object B ) , Obj B -> Obj C ( object B refers to object c ) and Obj C -> Obj B( object C again refers to Object B). So this is circular dependency.
So now when object A goes out of scope GC will collect this as object B refers by object A so this will also be collected but this also refers by Object C.... Grrrrrr lot of confusion huh ?
How GC takes care of this ?
GC is so smart. It doesn't bother about references. GC traverse by application roots now u must be thinking what are the application roots ?
Application roots are the entry points for GC and they referenced objects( if they exist otherwise null). So here is the list of application roots:
GC is a non deterministic process which runs only when it needs to run for a particular generation and collect those objects those are not marked.
Hope you enjoyed reading this article.
So now when object A goes out of scope GC will collect this as object B refers by object A so this will also be collected but this also refers by Object C.... Grrrrrr lot of confusion huh ?
How GC takes care of this ?
GC is so smart. It doesn't bother about references. GC traverse by application roots now u must be thinking what are the application roots ?
Application roots are the entry points for GC and they referenced objects( if they exist otherwise null). So here is the list of application roots:
- Static objects and static fields
- Global objects
- Reference on stack to method parameters
- Reference on stack to local objects
- Reference to objects that are in managed heap( CPU registers)
GC is a non deterministic process which runs only when it needs to run for a particular generation and collect those objects those are not marked.
Hope you enjoyed reading this article.