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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T16:51:42+00:00 2026-06-10T16:51:42+00:00

Hope you don’t mind, I may be missing something; I just need some clarification

  • 0

Hope you don’t mind, I may be missing something; I just need some clarification for the following scenario: If an object a contains a reference to a static list and also an entry in that static list, and object a falls out of scope, will it be garbage collected? Do I need to set object a’s reference to the static list and reference to the entry in that list to null before it becomes eligible?

I understand that the static list contains objects which will live for the lifetime of the application, so I was thinking since object a still references an entry in that static list, it is still in the main dependency object graph of objects which are still live?

Thanks in advance

  • 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-10T16:51:43+00:00Added an answer on June 10, 2026 at 4:51 pm

    Firstly, objects don’t fall out of scope, variables do. The difference is one of semantics most of the time, but vital here.

    Let’s create a concrete example of what you talk about:

    private static List<string> static_strings = new List<string>();//this won't be
                                                                    //collected unless we
                                                                    //assign null or another
                                                                    //List<string> to static_strings
    public void AddOne()
    {
      string a = new Random().Next(0, 2000).ToString();//a is in scope, it refers
                                                       //to a string that won't be collected.
      static_strings.Add(a);//now both a and the list are ways to reach that string.
      SomeListHolder b = new SomeListHolder(static_strings);//can be collected
                                                            //right now. Nobody cares
                                                            //about what an object refers
                                                            //to, only what refers to it.
    }//a is out of scope.
    public void RemoveOne()
    {
      if(static_strings.Count == 0) return;
      a = static_strings[0];//a is in scope.
      static_strings.RemoveAt(0);//a is the only way to reach that string.
      GC.Collect();//Do not try this at home.
      //a is in scope here, which means that we can write some code here
      //that uses a. However, garbage collection does not depend upon what we
      //could write, it depends upon what we did write. Because a is no
      //longer used, it is highly possible that it was collected because
      //the compiled code isn't going to waste its time holding onto referenes
      //it isn't using.
    }
    

    As seen here, scope is nothing, reachability is everything.

    In the case of an object that refers to a static, what it refers to is irrelevant, only what refers to it.

    In particular, note that this means that circular references do not prevent items from being collected, unlike with some reference-counted garbage collection approaches.

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

Sidebar

Related Questions

Hmm...this question may sounds silly , hope you all don't mind... If I have
Another synchronization question...I hope you guys don't get annoyed ;) Assume the following scenario:
I just want to confirm my understanding of a few fundamentals. Hope you don't
I hope i don't get slammed for asking something so basic. I could Google
I hope I don't lose anyone by mentioning Filemaker. I am trying to turn
Hope that someone can share their experience with some of the latest emerging backbone.js
hope someone can answer this. Here is some sample code. namespace std { #ifdef
This will take a bit of explanation so I hope I don't lose everyone
I don't think my question is difficult but I'm just a newbie in web
Hope you all are doing well. I need to import an XML-feed from a

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.