Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the CLR garbage collection methodology mean it's safe to throw circular object references around?

I have a theory that the CLR garbage collection mechanism means that I can get away with circular references in my object hierarchy without creating deadlocks for teardown and garbage collection. Is this a safe assumption to make? (Target language VB.NET)

like image 468
acheo Avatar asked Feb 03 '10 11:02

acheo


People also ask

Does garbage collector handle cyclic references?

yes Java Garbage collector handles circular-reference! How? There are special objects called called garbage-collection roots (GC roots). These are always reachable and so is any object that has them at its own root.

What does garbage collector do in CLR?

In the common language runtime (CLR), the garbage collector (GC) serves as an automatic memory manager. The garbage collector manages the allocation and release of memory for an application. Therefore, developers working with managed code don't have to write code to perform memory management tasks.

Which is not valid reference in terms of garbage collection?

Any object with at least one strong reference is not eligible for garbage collection. In our analogy, prefects hold strong references to their plates. In technical terms, we say the object is strongly reachable.

What is the role of reference counting in garbage collection?

The main advantage of the reference counting over tracing garbage collection is that objects are reclaimed as soon as they can no longer be referenced, and in an incremental fashion, without long pauses for collection cycles and with clearly defined lifetime of every object.


1 Answers

The .NET garbage collector is a generational mark and sweep collector. It does not use reference counting. So yes, it's safe to have circular references. Language is irrelevant

like image 139
Adrian Zanescu Avatar answered Nov 15 '22 00:11

Adrian Zanescu