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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:21:51+00:00 2026-05-28T18:21:51+00:00

So I know the basics here – an object is eligible for garbage collection

  • 0

So I know the basics here – an object is eligible for garbage collection when it’s no longer reachable by a root (i.e. a strong reference either from a local variable in a stack frame or a static reference)

The question I have is about this potential optimization where, even if an object is referenced from a local variable, it may be garbage collected at any point in a function where the variable is no longer referenced. First – it appears that existing implementations of C# don’t do this – both 2.0 and 4.0 seem to keep local references “live” until the stack frame is destroyed. But – I’d also like to write code that is still robust if and when garbage collection is optimized in later versions of the CLR.

So – without further ado, here’s some code illustration:

class Foo 
{
  ...
}
class Program
{
    public static void fxn1(int blah) 
    {
      ...
    }
    public static void fxn2(Foo foo)
    {
      ...
    }
    public static int ToInt(Foo foo)
    {
      ...
    }
    public static void Main()
    {
      ...
      Foo foo = new Foo();
      fxn2(foo); // I THINK foo may not be GC'ed until fxn2 returns...
        // I THINK foo may be GC'ed here, even though CLR2.0 and CLR4.0 don't...
        //  (experiment shows CLR 2.0 and 4.0 leave foo "live" until Main returns)
      fxn2(new Foo()); // I THINK the argument can't be GC'ed until fxn2 returns...
        // I KNOW that even CLR2.0 and CLR4.0 will GC the argument after the return...
      fxn1( ToInt(new Foo()) ); // I KNOW that new Foo is GC'able even within fxn1...
    }
}

So ultimately, the rules for existing CLR’s seem to be:
1. any object is “live” for the duration of a function call for which it is an immediate argument
2. any object is “live” for the duration of a function call if it is referenced by a local stack variable that is not reassigned. (even if the stack variable may not be referenced for several instructions at the end of the function)

However – apparently C# reserves the right to modify (2) so that an object is “live” up until the final use of a reference within a function.

Would this mean:

Foo foo = new Foo();
Foo foo2 = new Foo();
fxn2(foo); // foo is NOT GC'able until fxn1 returns?
   // foo IS GC'able from here on? (b/c no further uses of local "foo"?)
fxn2(foo2); // foo2 is NOT GC'able within fxn2 ?
fxn1(ToInt(foo2)); // foo2 IS GC'able within fxn1 ? (existing CLR does not GC foo2)

Is there anything in the ECMA spec which deals w/ garbage collection eligibility in detail?

  • 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-28T18:21:52+00:00Added an answer on May 28, 2026 at 6:21 pm

    @M.Babcock – Thank you for the link to the ECMA spec! 8.4 was actually too general, but the answer I was looking for was in 10.9 – and is identical to Java – when a variable can be no longer referenced by any possible future code path, then it is considered eligible for garbage collection – which means that although the existing clr implementation seems to scope local variable lifetime to the stack, there’s no guarantee that third party or future implementations will do so.

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

Sidebar

Related Questions

I want to know about Virtualization in detail. But start from basics, like what
I know the basics of clojure/java interop: calling java from clojure and vice versa.
I keep getting asked about AppDomains in interviews, and I know the basics :
I have seen the ASP.NET community buzzing about MVC. I know the basics of
I understand the basics of networking such as Lan and stuff. I know what
I'm familiar with some of the basics, but what I would like to know
First off, I'm not terribly experienced in XML. I know the very basics of
Here's the basics of the system we have in place today. We have a
i'm not really into basics algorithm programming, so here's my problem. I want to
I'm sure I am missing some of the basics here so please bear with

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.