[TestMethod]
public void Memory()
{
var wr = new WeakReference("aaabbb");
Assert.IsTrue(wr.IsAlive);
GC.Collect();
GC.Collect();
GC.Collect();
GC.Collect();
GC.Collect();
Assert.IsFalse(wr.IsAlive); //<-- fails here
}
It's .NET 3.5 SP1
Can anyone can tell me why this test fails?
Edit: Thanks stusmith
You have a reference to a string, which since it is a constant, is probably interned (ie not dynamically allocated), and will never be collected.
That was it. Changed first line to
var wr = new WeakReference(new object());
and the test passes :-)