Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8998419
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:01:27+00:00 2026-06-16T00:01:27+00:00

Here’s an academic question about object finalization and collection in C#/.NET. Background reading is

  • 0

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();   
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T00:01:28+00:00Added an answer on June 16, 2026 at 12:01 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here are some facts about my app followed by a question My app has
Here's a newbie question, but how do I use dates for the x-axis in
Here is a great article about GC may occur at unexpected point of code
Here is a scenario: User installs .NET application that you have made. After some
Here is an object that I'd like to use with ng-repeat, but it's not
Here is a small demonstration of what my problem is: http://jsfiddle.net/k2Pqw/4/ After sizing the
Here a simple question : What do you think of code which use try
I am reading a book about Javascript and jQuery and using one of the
Here's the view: @if (stream.StreamSourceId == 1) { <img class=source src=@Url.Content(~/Public/assets/images/own3dlogo.png) alt= /> }
Here's my code in the <head></head> : <link rel=stylesheet href=http://code.jquery.com/mobile/1.1.0/jquery.mobile-1.1.0.min.css /> <script type=text/javascript src=http://code.jquery.com/jquery-1.7.1.min.js></script>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.