2

I have another C# theory question I am hoping I can get some clarity on, I have seen a few WeakReference samples around but they never work for me but I've read in some people's comments and articles that the samples work for them. I am battling to find out why these samples are not working for me. I can't tell if it is non-deterministic behavior of GC.Collect(), which I'm also battling to establish if that is even applicable. This is the code I'm working on at the moment but I have tried numerous others directly from tutorials that illustrate the concept as well:

class Program
{
    static WeakReference _weak;

    static void Main(string[] args)
    {
        _weak = new WeakReference(new WeakClass { Name = "Matthew" });

        if (_weak.IsAlive)
        {
            Console.WriteLine((_weak.Target as WeakClass).ToString());
        }

        GC.Collect();

        if (_weak.IsAlive)
        {
            Console.WriteLine("IsAlive"); // This is always being printed when, according to the articles, it shouldn't be
        }

        Console.WriteLine("[Done]");
        Console.Read();
    }
}
class WeakClass
{
    public string Name { get; set; }
    public override string ToString()
    {
        return this.Name;
    }
    ~WeakClass()
    {
        Console.WriteLine(string.Format("{0} got destructed...", this.Name));
    }
}

The WeakRerence is always still alive after I call GC.Collect(). I've also tried adding calls to GC.WaitForFullGCComplete() and GC.WaitForPendingFinalizers() as well with no joy.

macmatthew
  • 302
  • 2
  • 9

1 Answers1

3

I'm assuming you're running this in Debug mode, where the runtime isn't as eager to collect unreferenced variables and doesn't optimize your code in order for you to be able to debug your application.

If you compile and run this same code in Release mode, you'll usually see that the second call to WeakReference.IsAlive will yield false after GC.Collect.

This is what I see running LINQPad 5 in Release Mode:

Matthew
[Done]
Matthew got destructed...
Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321
  • Thank you very much. I've just tried building in Release mode and running from the bin\Release folder and it works now. This has been frustrating me for a while now, thank you very much. Just out of interest sake, is there a reason for the runtime not being as consistent during Debug mode or is this just something I must be aware of to always test in Release mode? – macmatthew Mar 06 '16 at 11:20
  • @macmatthew I suggest you read [Debug/Release](http://stackoverflow.com/questions/367884/what-is-the-difference-between-debug-and-release-in-visual-studio) in order to get more insights on the difference between the two. It's not about inconsistency, it's the fact that debug mode, as it's name says, is there to aid you debug your code. – Yuval Itzchakov Mar 06 '16 at 11:22
  • thank you very much for the suggestion, I will definitely read that article! Thanks again for helping me with this. I was getting nowhere with my research. – macmatthew Mar 06 '16 at 11:26