Reputation: 50970
In the following code:
public class MyClass
{
public SmallObject SmallThing { get; set; }
private void MyMethod()
{
var largeThing = new LargeObject();
SmallThing = largeThing.SmallThing;
}
}
after MyMethod()
has executed I have a reference to SmallThing
but no reference to largeThing
. Is Dot Net able to recognize that largeThing
is eligible to be collected while the SmallObject
instance it contained is not so eligible? Or does Dot Net somehow remember the original association between largeThing
and SmallThing
? (You may assume that SmallThing
does not contain an explicit backward reference to largeThing
created by the programmer).
Upvotes: 1
Views: 68
Reputation: 127563
You are correct, largeThing
will be collectable as soon as you pass the line
SmallThing = largeThing.SmallThing;
it does not need to even wait for MyMethod
to return for it to be able to collect it (This assumes running release mode without the debugger attached). As soon as the last useage of largeThing
is passed it will be eligible for collection.
Upvotes: 1
Reputation: 152566
Since there are no active references to largeThing
it will be eligible for garbage collection. There are no "hidden" or implicit references back from SmallThing
that would keep it from being eligible.
Upvotes: 1