Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to create an "Island of Isolation" scenario in .NET?

I have been reading about garbage collection and came to know the term "Island Of Isolation", such as when ObjectA references to ObjectB and ObjectB simultaneously references to ObjectA.

Can someone give me an example of this in C#? Also, can you please explain if this is the same thing as a "memory leak"?

like image 888
Asdfg Avatar asked May 18 '11 14:05

Asdfg


2 Answers

The island of Isolation is typically a problem in GCs that use reference counters. ObjectA and ObjectB in the scenario you described so both have a reference count of 1 and so won't be collected even though nothing else can reach it.

This however doesn't happen in .NET because .NET's GC uses a mark and sweep algorithm. It starts at the roots and creates an object graph so only items that are rooted will survive a collection. This means nothing can be "Isolated"

like image 106
Conrad Frix Avatar answered Oct 14 '22 02:10

Conrad Frix


The garbage collector in .Net doesn't use reference counting to know when it can collect an object. Instead, it builds an object graph where objects that are currently accessible (read: in scope or global) are at the root of the graph. It then uses the graph to see whether each object in memory is connected to the root; anything that is not connected to the root is said to be "unrooted" and can be collected. This is true even if other objects have references to an unrooted object, as long as those objects are also not rooted. This way, that "Island of Isolation" you're talking about just isn't a problem.

like image 31
Joel Coehoorn Avatar answered Oct 14 '22 03:10

Joel Coehoorn