11

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"?

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Asdfg
  • 11,362
  • 24
  • 98
  • 175
  • 1
    Not a problem in .NET. Where did you read about this? – Cody Gray - on strike May 18 '11 at 14:26
  • @Cody Gray: This is where i read it and i was wondering how it can happen in .net: http://stackoverflow.com/questions/483427/garbage-collection-operation/483754#483754 – Asdfg May 18 '11 at 14:32
  • Hmmm, that's the problem with overly vague questions that somehow ended up tagged with multiple distinct languages... Not applicable at all to the .NET CLR. I don't know anything at all about Java, but I assume its garbage collector uses a completely different algorithm. – Cody Gray - on strike May 18 '11 at 14:36
  • Thank you all for helping me to understand this. I appreciate your help. – Asdfg May 18 '11 at 14:47
  • 4
    Definitely **not** a dupe... That question is about Java, this one's about .NET. Don't let the title fool you, that's the same mistake the asker here made. – Cody Gray - on strike May 19 '11 at 11:44
  • i dont mind you closing the question, but may be you did not notice that question was asked for .NET and not for Java. In Both the languages, concept of "island of isolation" is very different. – Asdfg May 19 '11 at 14:04

4 Answers4

16

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"

Conrad Frix
  • 51,984
  • 12
  • 96
  • 155
  • 1
    Nice. Instead of *'This means nothing can be "Isolated"'*, I'd prefer "Find all the non-isolated and kill the rest" – ypercubeᵀᴹ Jan 24 '12 at 21:14
  • @ypercube good point that is clearer, but I hesitate to edit as it was already closed as dupe and its a bit of a bike shed. – Conrad Frix Jan 24 '12 at 21:24
7

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.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
3

Example from: When is an object eligible for garbage collection?

class Person {
   public Person firend;
   public static void main(String[] args) {
     Person obj1 = new Person();
     Person obj2 = new Person();
     obj2.firend = obj1;

     obj1 = null;  //Line A
     obj2 = null;  //Line B

     .....
   }
}

After Line A executes, The object obj2 still has a reference to the object obj1 and the object obj2 is still referenced by the variable obj2. Therefore, the object obj1 can not be eligable for garbage collection. After Line B exectues, there are no more references to the object obj2. There still is a reference to object obj1 inside the object obj2. Now, the object obj1 and obj2 has no reference from root set of references. Therefore, both of objects are eligible for garbage collection.

Teoman Soygul
  • 25,584
  • 6
  • 69
  • 80
  • 1
    Doesn't matter even if does, example simple shows that as soon as both objects are nulled, they will be eligible for GC so there is no isolation problem. – Teoman Soygul May 18 '11 at 14:31
  • Hmm... I guess I misread your explanation. Upon a closer examination, it appears that it's just poorly worded. The last sentence is mostly correct, at least as far as the term "eligible" can be stretched here. But the way you lead up to it is confusing at best. – Cody Gray - on strike May 18 '11 at 14:33
  • 3
    Technically, obj1 and obj2 become eligible for collection as soon as the compiler knows that they are no longer used, regardless of whether you've set the value to null. The GC is allowed to be quite aggressive. This is valuable, since it's not uncommon to have a lot of objects allocated within a method and have them still 'in scope', even though the rest of the code no longer uses them. Consider a temporary list created to do some computation, after which the method goes on to do other things; the list is eligible for collection as soon as you stop using it. – Dan Bryant May 18 '11 at 14:33
3

Well that certainly doesn't leak in .NET. For example:

public class Foo
{
    public Foo Bar { get; set; }
}

...

static void Main()
{
    Foo f1 = new Foo();
    Foo f2 = new Foo();
    f1.Bar = f2;
    f2.Bar = f1;

    // Not actually required so long as nothing in the rest of the method
    // references f1 and f2
    f1 = null;
    f2 = null;

    // The two Foo objects refer to each other, but they're both eligible for
    // garbage collection.
}

In a ref-counted system this would leak memory - in the .NET GC, it doesn't. When the GC runs, it will find all objects reachable from known roots; anything unreachable can be garbage collected.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194