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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T19:46:31+00:00 2026-05-14T19:46:31+00:00

See also these related resources: Does the .NET garbage collector perform predictive analysis of

  • 0

See also these related resources:

  • Does the .NET garbage collector perform predictive analysis of code? (on Stack Overflow)
  • WP7: When does GC Consider a Local Variable as Garbage (blog article on MSDN)

In other words:

Can an object referenced by a local
variable be reclaimed before the
variable goes out of scope (eg.
because the variable is assigned, but
then not used again), or is that
object guaranteed to be ineligible for
garbage collection until the variable
goes out of scope?

Let me explain:


void Case_1()
{
    var weakRef = new WeakReference(new object());

    GC.Collect();  // <-- doesn't have to be an explicit call; just assume that
                   //     garbage collection would occur at this point.

    if (weakRef.IsAlive) ...
}

In this code example, I obviously have to plan for the possibility that the new’ed object is reclaimed by the garbage collector; therefore the if statement.

(Note that I’m using weakRef for the sole purpose of checking if the new’ed object is still around.)


void Case_2()
{
    var unusedLocalVar = new object();
    var weakRef = new WeakReference(unusedLocalVar);

    GC.Collect();  // <-- doesn't have to be an explicit call; just assume that
                   //     garbage collection would occur at this point.

    Debug.Assert(weakRef.IsAlive);
}

The main change in this code example from the previous one is that the new’ed object is strongly referenced by a local variable (unusedLocalVar). However, this variable is never used again after the weak reference (weakRef) has been created.


Question: Is a conforming C# compiler allowed to optimize the first two lines of Case_2 into those of Case_1 if it sees that unusedLocalVar is only used in one place, namely as an argument to the WeakReference constructor? i.e. is there any possibility that the assertion in Case_2 could ever fail?

  • 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-14T19:46:32+00:00Added an answer on May 14, 2026 at 7:46 pm

    It doesn’t matter what the C# compiler does – the JITter/GC are allowed to clean up local references once they’re no longer alive in a method body. Look at the docs for GC.KeepAlive

    Also, this powerpoint presentation, especially from slide 30 onwards, helps to explain what the JIT/GC can get up to.

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

Sidebar

Related Questions

(Note: See also the related question Can browsers react to Set-Cookie specified in headers
See also SO 1664677 I get this error 2: Syntax error: ( unexpected when
see also Differences between LINQ to Objects and LINQ to SQL queries We are
See also: Emacs behind HTTP proxy Is it possible to tell emacs to automatically
What are "iterable", "iterator", and "iteration" in Python? How are they defined? See also:
I have 3 main elements in my XML layout (also see the image): The
I know you can print with printf() and puts() . I can also see
st = What kind of speCialist would we see for this?He also seems to
See here: http://jsfiddle.net/zYcJm/ The jQuery should be validating each field to check if they
In comments in source code (especially older code), one may see in the header

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.