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 4042990
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T13:04:23+00:00 2026-05-20T13:04:23+00:00

In his blog, When does an object become available for garbage collection? , Reymond

  • 0

In his blog, When does an object become available for garbage collection?, Reymond Chen writes that

An object can become eligible for
collection during execution of a
method on that very object.

Also, Curt Nichols demonstrates the same point through this example

public class Program
    {
        static void Main(string[] args)
        {
            new TestClass().InstanceMethod();

            Console.WriteLine("End program.");
            Console.ReadLine();
        }
    }

    public sealed class TestClass
    {
        private FileStream stream;

        public TestClass()
        {
            Console.WriteLine("Ctor");

            stream = new FileStream(Path.GetTempFileName(), FileMode.Open);
        }

        ~TestClass()
        {
            Console.WriteLine("Finializer");

            stream.Dispose();
        }

        public void InstanceMethod()
        {
            Console.WriteLine("InstanceMethod");

            StaticMethod(stream);
        }

        private static void StaticMethod(FileStream fs)
        {
            GC.Collect();
            GC.WaitForPendingFinalizers();

            Console.WriteLine("StaticMethod"); 
            var len = fs.Length;
        }
    }

The output is as expected –

Ctor
InstanceMethod
Finalizer
StaticMethod

ObjectDisposedException is thrown

In this example, I am not able to understand, how GC could collect the temporary TestClass object since its member stream was being referred to by the StaticMethod.

Yes, Raymond states that

GC is not about tracing roots but
about removing objects that are no
more in use

However, in this example TestClass object is still being used, isn’t it?

Please explain how GC is right in collecting TestClass object in this case? Also, more importantly, how should developers safeguard against these situations?

  • 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-05-20T13:04:23+00:00Added an answer on May 20, 2026 at 1:04 pm

    I am not able to understand, how GC
    could collect the temporary TestClass
    object since its member stream was
    being referred to by the StaticMethod.

    StaticMethod is in fact not holding onto a reference the TestClass instance’s stream member – it’s holding onto a reference to (as far as the GC is concerned) some FileStream object on the heap.

    Note the default pass-by-value semantics of C#. In this statement:

    StaticMethod(stream);
    

    A copy of the value of the stream field is passed as an argument to the static method. FileStream is a reference-type and the value of a reference-type expression is a reference. Hence, a reference to a FileStream object on the heap is passed to the method, not (as you appear to be thinking) a reference / interior reference to a TestClass object

    The fact that this FileStream object is still reachable when GC.Collect is called does not result in making the TestClass object (that created it and has a reference to it through a field) also reachable. “Live” objects make other objects live by referencing them, not by being referred to by them.

    Assuming optimizations that result in unneeded references being aggressively “popped off”, let’s look at the reachability of the created TestClass object.

    • Main doesn’t need a reference to it after it calls:
    • InstanceMethod, which in turn doesn’t need the implicitly passed this reference after it dereferences it to read the stream field. The object becomes eligible for collection at this point. It then calls:
    • StaticMethod, which in turn (as mentioned earlier) isn’t holding to a reference to the object at all.

    Consequently, the TestClass object isn’t needed right after after its stream field is read, and is most certainly eligible for collection while StaticMethod is executing.

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

Sidebar

Related Questions

My co-worker posted a question on his blog . He is referring to rendering
In his PDC talk, Anders said that the dynamic keyword would dispatch any function
Bjarne Stroustrup writes in his C++ Style and Technique FAQ , emphasis mine: Because
Why does a user needs privileges over his own schema to create packages and
Use case: A does something on his box and gots stuck. He asks B
I have a colleague who insists that his code doesn't need comments, it's "self
In his The C++ Programming Language Stroustrup gives the following example for inc/dec overloading:
Recently Jeff has posted regarding his trouble with database deadlocks related to reading. Multiversion
I have a business user who tried his hand at writing his own SQL
My dad called me today and said people going to his website were getting

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.