2

I'm trying to consistently force objects to be garbage collected, for purposes of writing unit-tests related to weak references. However, GC.Collect(), which I expect to force garbage collection, does not seem to behave consistently. Consider the following example code.

static void Main (string [] args)
{
    var obj = new object ();
    var wRef = new WeakReference (obj);

    obj = null;
    GC.Collect ();
    Console.WriteLine (wRef.IsAlive); // Prints false.
    Console.ReadKey ();
}

In the code above the object seems to be collected, while in the code below this is not the case, although the code is virtually the same.

static void Main (string [] args)
{
    var obj = new object ();
    var wRef = new WeakReference (obj);

    obj = null;
    GC.Collect ();
    bool alive = wRef.IsAlive ? true : false;
    Console.WriteLine (alive); // Prints true
    Console.ReadKey ();
}

Can someone explain this behavioral difference, and show how we can reliably have objects getting reclaimed in order to do some testing with weak references?

Update

When running the second snippet in release mode without a debugger attached (i.e. outside Visual Studio) it prints False.

Community
  • 1
  • 1
chtenb
  • 14,924
  • 14
  • 78
  • 116
  • I'm not sure as to why it is happening, though I *can* say, it seems to behave as expected in Release mode without a debugger attached. Otherwise the second snippet always returns true. – Glorin Oakenfoot Dec 21 '15 at 15:38
  • 2
    Make sure you run it *outside* Visual Studio. For me, with the debugger attached (i.e. VS), it returns true. Without the debugger attached it returns false. – Glorin Oakenfoot Dec 21 '15 at 15:41
  • It ought to be sufficient to run the Release config 'without debugger'. But you should really test this with your Unit Test framework. – H H Dec 21 '15 at 15:45
  • I know the JIT knows when a debugger is attached and will handle things differently. Not sure as to what *specifically* is causing your issue here. Seems to be something along that path, though. – Glorin Oakenfoot Dec 21 '15 at 15:45
  • @HenkHolterman In NUnit it behaves exactly the same. Needless to say, it has too many drawbacks to always have to run your unittests in release mode without a debugger. – chtenb Dec 21 '15 at 15:51
  • Just go to the properties of the test Project and enable Optimization in all configurations. And accept that those tests fail while debugging them. Shouldn't be a big deal. – H H Dec 21 '15 at 16:30
  • Possible duplicate of [Garbage Collection should have removed object but WeakReference.IsAlive still returning true](http://stackoverflow.com/questions/15205891/garbage-collection-should-have-removed-object-but-weakreference-isalive-still-re) – Ian Ringrose Feb 08 '17 at 15:48

0 Answers0