I have a class that holds on to a delegate, in order to lazily evaluate something later.
Once I've evaluated it, by calling the delegate, I clear out the reference to the delegate, hoping that it would be eligible for collection. After all, it might hold on to a world of local variables if it was constructed as an anonymous method.
I tried building a unit-test to verify this, but it doesn't seem to work out the way I planned, instead it seems that either my assumptions about WeakReference
(which I used for test purposes here), or some other assumption, doesn't hold water.
Take a look at this code, which you can run in LINQPad
void Main()
{
WeakReference wr;
Lazy<int> l;
CreateTestData(out wr, out l);
wr.IsAlive.Dump(); // should be alive here
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
wr.IsAlive.Dump(); // and alive here as well
l.Value.Dump(); // but now we clear the reference
GC.Collect(); // so one of these should collect it
GC.WaitForPendingFinalizers();
GC.Collect();
wr.IsAlive.Dump(); // and then it should be gone here
GC.KeepAlive(l);
}
void CreateTestData(out WeakReference wr, out Lazy<int> l)
{
Func<int> f = () => 10;
wr = new WeakReference(f);
l = new Lazy<int>(f);
}
public class Lazy<T>
{
private Func<T> _GetValue;
private T _Value;
public Lazy(Func<T> getValue)
{
_GetValue = getValue;
}
public T Value
{
get
{
if (_GetValue != null)
{
_Value = _GetValue();
_GetValue = null;
}
return _Value;
}
}
}
I was assuming that:
- Regardless of DEBUG build, debugger attached, since I created the delegate in a separate method, which I return from, there should be nothing holding on to the delegate, except the
WeakReference
and theLazy<T>
objects - If I ask the
Lazy<T>
object to relinquish its reference to the delegate, which would reduce the references to only the one thatWeakReference
is holding on to - And then force a full garbage collection, assuming that if the only reference left is the one in
WeakReference
- Then the delegate would be collected, and my
WeakReference
would indicate that the object is no longer alive
The output of the code was thus expected to be (with comments):
true // not gc'ed after construction
true // not gc'ed after full GC, still beind held by Lazy<T>
10 // value from calling delegate
false // but is now gc'ed, Lazy<T> no longer has a reference to it
But instead the output is:
true
true
10
true
Can anyone shed some light on what I'm missing here?