Garbage Collection Behavior on Different Platforms

TomBulatewiczTomBulatewicz USMember
edited October 2016 in Xamarin.iOS

Hello! I'd like to understand why the following code produces different output when you run it as a console Mac app via XS vs. a Windows app via VS. On Windows it outputs "True, False" and on Mac it outputs "True, True":

using System;

namespace MemoryFun
{
    class Dog
    {
        public Dog(string name)
        {
        }
    }

    class MainClass
    {
        public static void Main(string[] args)
        {
            Dog dog = new Dog("Bowser");

            WeakReference dogRef = new WeakReference(dog);
            Console.WriteLine(dogRef.IsAlive);

            dog = null;
            GC.Collect();

            Console.WriteLine(dogRef.IsAlive);
        }
    }
}

Thanks!
-Tom B.

Tagged:

Answers

  • ChrisHamonsChrisHamons USForum Administrator, Xamarin Team Xamurai

    This sounds like a behavior difference between Microsoft's .NET implementation and mono's.

    I can reproduce your behavior in a simple console application (without Xamarin.Mac).

    I'm not an expert in this area, may I suggest you post on the mono mailing list?

    http://lists.ximian.com/mailman/listinfo/mono-list

    You might get more specific details there.

Sign In or Register to comment.