Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WeakReference Behavior When Object Is Finalized But Not Yet Garbage Collected

Here's an academic question about object finalization and collection in C#/.NET. Background reading is section 3.9 of the C# language spec, Automatic Memory Management.

When there are no explicit references to an object, it may become garbage collected. It becomes "eligible for destruction". At some point in the future (e.g. if you force garbage collection), the object's destructor will be run.

In the destructor, if you save a reference to the object, the object will be finalized, but will not be eligible for collection. This can lead to an object being in a state where it has been finalized, but not collected. Sec 3.9 of the spec has an example of this.

At this point, the object is really still alive, since it has not yet been garbage collected. However, a WeakReference referring to the object reports an IsAlive value of false, indicating that the object has been collected.

The core question is this--what is the IsAlive property really reporting? We know that we can't trust a value of true for this property, because the value can become false shortly after you read it. But a value of false is trustworthy and is meant to indicate (according to documentation) that the object has been garbage collected. So what is the IsAlive property telling us in this case? Not strictly whether the object has been garbage collected, since we believe that the object is in a finalized-but-not-collected state.

Here's a sample to show the behavior.

    public class Dog 
    {
        public static Dog KeepDogRef;



   public string Name { get; set; }

    public Dog(string name)
    {
        Name = name;
    }

    ~Dog()
    {
        Console.WriteLine("Dog destructor for " + Name + " called");
        Dog.KeepDogRef = this;
    }

    public void Bark()
    {
        Console.WriteLine(Name + " : Woof");
    }
}

And code for main program. If you run the code, you'll see that the original WeakReference reports IsAlive as false, even after we reconstitute the object.

    static void Main()
    {
        Dog dog = new Dog("Bowser");

        WeakReference dogRef = new WeakReference(dog);

        // Unref Bowser, now eligible for destruction
        dog = null;
        GC.Collect();
        GC.WaitForPendingFinalizers();

        // Bowser no longer alive
        Console.WriteLine(string.Format("Object still alive: {0}", dogRef.IsAlive));

        // Bowser alive again
        Dog newRef = Dog.KeepDogRef;
        newRef.Bark();   
    }
}
like image 388
Sean Sexton Avatar asked Dec 17 '12 13:12

Sean Sexton


People also ask

When should Weakreference be used in garbage collection?

A weak reference permits the garbage collector to collect the object while still allowing the application to access the object. A weak reference is valid only during the indeterminate amount of time until the object is collected when no strong references exist.

What is weak reference garbage collection in case of such references?

A weakly referenced object is cleared by the Garbage Collector when it's weakly reachable. Weak reachability means that an object has neither strong nor soft references pointing to it. The object can be reached only by traversing a weak reference.

What is the purpose of weak reference c#?

A weak reference allows the garbage collector to collect an object while still allowing an application to access the object. If you need the object, you can still obtain a strong reference to it and prevent it from being collected.


1 Answers

If you read all of the documentation for WeakReference, it's clear that there is more than one type of weak reference available. The default is to produce a short weak reference. But you can also create long weak references which specifically account for resurrection scenarios.

From the documentation for TrackResurrection:

Gets an indication whether the object referenced by the current WeakReference object is tracked after it is finalized.

If true, the weak reference is a long weak reference and true was specified for the trackResurrection parameter in the WeakReference constructor.

So I'd say you have to understand this part of weak references before interpreting the IsAlive property.

like image 164
Damien_The_Unbeliever Avatar answered Sep 20 '22 22:09

Damien_The_Unbeliever