+12  A: 

The Garbage Collector relies on information compiled into your assembly provided by the JIT compiler that tells it what code address ranges various variables and "things" are still in use over.

As such, in your code, since you no longer use the object variables GC is free to collect them. WeakReference will not prevent this, in fact, this is the whole point of a WR, to allow you to keep a reference to an object, while not preventing it from being collected.

The case about WeakReference objects is nicely summed up in the one-line description on MSDN:

Represents a weak reference, which references an object while still allowing that object to be reclaimed by garbage collection.

The WeakReference objects are not garbage collected, so you can safely use those, but the objects they refer to had only the WR reference left, and thus were free to collect.

When executing code through the debugger, variables are artificially extended in scope to last until their scope ends, typically the end of the block they're declared in (like methods), so that you can inspect them at a breakpoint.

There's some subtle things to discover with this. Consider the following code:

using System;

namespace ConsoleApplication20
{
    public class Test
    {
        public int Value;

        ~Test()
        {
            Console.Out.WriteLine("Test collected");
        }

        public void Execute()
        {
            Console.Out.WriteLine("The value of Value: " + Value);

            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();

            Console.Out.WriteLine("Leaving Test.Execute");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Test t = new Test { Value = 15 };
            t.Execute();
        }
    }
}

In Release-mode, executed without a debugger attached, here's the output:

The value of Value: 15
Test collected
Leaving Test.Execute

The reason for this is that even though you're still executing inside a method associated with the Test object, at the point of asking GC to do it's thing, there is no need for any instance references to Test (no reference to this or Value), and no calls to any instance-method left to perform, so the object is safe to collect.

This can have some nasty side-effects if you're not aware of it.

Consider the following class:

public class ClassThatHoldsUnmanagedResource : IDisposable
{
    private IntPtr _HandleToSomethingUnmanaged;

    public ClassThatHoldsUnmanagedResource()
    {
        _HandleToSomethingUnmanaged = (... open file, whatever);
    }

    ~ClassThatHoldsUnmanagedResource()
    {
        Dispose(false);
    }

    public void Dispose()
    {
        Dispose(true);
    }

    protected virtual void Dispose(bool disposing)
    {
        (release unmanaged resource here);
        ... rest of dispose
    }

    public void Test()
    {
        IntPtr local = _HandleToSomethingUnmanaged;

        // DANGER!

        ... access resource through local here
    }

At this point, what if Test doesn't use any instance-data after grabbing a copy of the unmanaged handle? What if GC now runs at the point where I wrote "DANGER"? Do you see where this is going? When GC runs, it will execute the finalizer, which will yank the access to the unmanaged resource out from under Test, which is still executing.

Unmanaged resources, typically accessed through an IntPtr or similar, is opaque to the garbage collector, and it does not consider these when judging the life of an object.

In other words, that we keep a reference to the handle in a local variable is meaningless to GC, it only notices that there are no instance-references left, and thus considers the object safe to collect.

This if course assumes that there is no outside reference to the object that is still considered "alive". For instance, if the above class was used from a method like this:

public void DoSomething()
{
    ClassThatHoldsUnmanagedResource = new ClassThatHoldsUnmanagedResource();
    ClassThatHoldsUnmanagedResource.Test();
}

Then you have the exact same problem.

(of course, you probably shouldn't be using it like this, since it implements IDisposable, you should be using a using-block or calling Dispose manually.)

The correct way to write the above method is to enforce that GC won't collect our object while we still need it:

public void Test()
{
    IntPtr local = _HandleToSomethingUnmanaged;

    ... access resource through local here

    GC.KeepAlive(this); // won't be collected before this has executed
}
Lasse V. Karlsen
Shouldn't uses of the `WeakReference` pointers be ignored for the purposes of garbage collection though?
jdmichal
@Lasse, +1, very instructive answer. I'm waiting a little before I accept it, in case some extra information comes up ;). @jdmichal, indeed, and apparently they *are* ignored, since I use the WRs after the `GC.Collect`
Thomas Levesque
Weak references are there to specifically allow you to keep a reference to an object, while not preventing it from being collected. As such, the reference is ignored when doing a GC. The whole point of a WR is to ensure that objects that could be collected *can* be collected, but if they're so costly to create that you'd rather avoid them being collected, use a normal reference instead. I don't, however, know if there is any special handling of weak references, that is, if their chance of being collected is different from normal objects.
Lasse V. Karlsen
Why don't the Console.WriteLine lines, referencing the WeakReference objects, extend their life?
Overhed
The WeakReference objects are kept alive, the objects they refer to are not. That's the whole point of WeakReference, not preventing GC from collecting the objects.
Lasse V. Karlsen
Very complete and well explained answer, thanks !
Thomas Levesque
Lasse: Weakreferences are not really designed for caching; they're designed for situations where an object needs to be able to manipulate an object that some other (unknown) object(s) might be "interested" in, but the manipulating objects have no real "interest" of their own. For example, one object may view something as a write-only data sink, and another may view it as a data source. If the writer only holds a weak reference except when actually writing, the object can be jettisoned if all readers go away.
supercat
That is a very good example of the purpose of a weak reference. You're entirely correct in that WR's are not good for caching, as you lose all control over the lifetime of an object.
Lasse V. Karlsen
On the DoSomething example, did you mean var classThatHoldsUnmanagedResource = new ClassThatHoldsUnmanagedResource(); classThatHoldsUnmanagedResource.Test();?
TrueWill
"that we keep a reference to the handle in a local variable is meaningless to GC" - but we aren't keeping a reference. IntPtr is a struct; we're making a copy. The actual internal handle is unmanaged; GC has no knowledge of it. If the handle were a reference type, it would not be finalized until after the local access (I tested this). Rather than calling GC.KeepAlive(this), I'd call GC.KeepAlive(_HandleToSomethingUnmanaged) as per http://msdn.microsoft.com/en-us/library/system.gc.keepalive.aspx . This works.
TrueWill
Which is just another reason to use SafeHandle. http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.safehandle.aspx
TrueWill
+8  A: 

The garbage collector gets lifetime hints from the JIT compiler. It thus knows that at the GC.Collect() call there are no more possible references to the local variables and that they therefore can be collected. Review GC.KeepAlive()

When a debugger is attached, JIT optimization is disabled and the lifetime hint gets extended to the end of the method. Makes debugging a lot simpler.

Hans Passant
That's in substance the same answer as Lasse's, with a notable difference : you say the lifetime hint is provided by the JIT, but Lasse says that it's in the assembly... Who should I believe ? ;). +1 anyway, since it seems close enough to the real explanation
Thomas Levesque
He's right about JIT providing this information. I'll edit my answer.
Lasse V. Karlsen
A: 

I am not an C# expert but I would say that this is because in production your code is optimize in

static void TestGC()
{
        WeakReference w1 = new WeakReference(new Object());
        WeakReference w2 = new WeakReference(new Object());

        GC.Collect();

        Console.WriteLine("o1 is alive: {0}", w1.IsAlive);
        Console.WriteLine("o2 is alive: {0}", w2.IsAlive);
}

No more o1, o2 binding remain.

EDIT: This is a Compiler optimization named constant folding. Which can be done with or without a JIT.

If you have a way to disable the JIT but kept the release optimization you're gonna have the same behavior.

People should pay attention to the note:

NOTE : this occurs only when the code is compiled in Release mode and run outside the debugger

this is the key.

PS: I am also assuming that your understand what WeakReference mean.

mathk
But `w1` and `w2` still reference something, even if you take away the name. The point is that the weakly referenced objects have been collected when the compiler can prove they will no longer be used.
Ben Voigt
Pay attention of what I say. I am not talking of name I am talking of binding. And your are wrong w1 and w2 are not collected but the object point by w1 and w2 are. If w1 and w2 were collected `w2.IsAlive` would throw a NullPointerException which is not the case.
mathk