Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I got an example online to test weak reference. why is this _weak not garbage collected?

What I have tried:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestWeakReference2
{
 
    class Program
    {
        /// <summary>
        /// Points to data that can be garbage collected any time.
        /// </summary>
        //static WeakReference _weak;

        static void Main(string[] args)
        {
            // Assign the WeakReference.
            Dog sb = new Dog("Bowser");

            WeakReference _weak = new WeakReference(sb);

            // See if weak reference is alive.
            if (_weak.IsAlive)
            {
                Console.WriteLine((_weak.Target as Dog).ToString());
            }

            // Invoke GC.Collect.
            // ... If this is commented out, the next condition will evaluate true.
            sb = null;
            GC.Collect();

            Console.WriteLine(_weak.IsAlive.ToString());


            // Check alive.
            if (_weak.IsAlive)
            {
                Console.WriteLine("IsAlive");
            }
            else
            {
                Console.WriteLine("it is garbage collected!");
                
            }

            // Finish.
            Console.WriteLine("[Done]");
            Console.Read();
        }
    }


    public class Dog
    {
        private string name;

        public Dog() : this("noname")
        {

        }
        public Dog(string name)
        {
            this.name = name;
        }

    }

}
Posted
Updated 30-Nov-19 17:25pm

1 solution

I think the if condition added for checking alive is culprit here the condition somehow does allow GC to collect weakreferences here.
below code should work for you

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestWeakReference2
{
 
    class Program
    {
        /// <summary>
        /// Points to data that can be garbage collected any time.
        /// </summary>
        //static WeakReference _weak;

        static void Main(string[] args)
        {
            // Assign the WeakReference.
            Dog sb = new Dog("Bowser");

            WeakReference _weak = new WeakReference(sb);

            // See if weak reference is alive.
            if (_weak.IsAlive)
            {
                Console.WriteLine((_weak.Target as Dog).ToString());
                Console.WriteLine(_weak.IsAlive.ToString());
            }

            // Invoke GC.Collect.
            // ... If this is commented out, the next condition will evaluate true.
            sb = null;
            GC.Collect();

            Console.WriteLine(_weak.IsAlive.ToString());

            // Finish.
            Console.WriteLine("[Done]");
            Console.Read();
        }
    }


    public class Dog
    {
        private string name;

        public Dog() : this("noname")
        {

        }
        public Dog(string name)
        {
            this.name = name;
        }

    }

}


removed the if condition to alive after GC.collect and it should be fine
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900