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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:40:04+00:00 2026-05-22T23:40:04+00:00

I have this code classObject var1 = new classObject(); classObject var2 = var1; Now,

  • 0

I have this code

 classObject var1 = new classObject();
 classObject var2 = var1;

Now, I wrote this:

 var1 = null;

How the GC will handle this ?
Does GC will collect the instance of this classObject ?

And how GC will hangle this if i will wrote var2= null ?
( and remove the var1=null from the code )

  • 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-22T23:40:05+00:00Added an answer on May 22, 2026 at 11:40 pm

    Short answer: no, because var2 still refers to the instance. As long as you have at least one reference to an object, the object will not be collected.

    Longer answer: actually, probably it will in this example, because you don’t go on to do anything with var2. The GC takes variable liveness into account. If you never use var2 again in the code, var2 is considered not to be live after that last line of code there. And so such a reference is considered not to be meaningful for garbage collection purposes – it’s as though var2 no longer exists. So in fact, the object will be eligible for collection:

    Additional subtleties: it will also depend on whether you’re running in debug or release mode. When debugging, .NET artificially extends the liveness of variables to extend to the end of their lexical scope, so that you have the opportunity to inspect local variables. Also, of couse, objects will only be collected when the GC actually runs.

    So strictly speaking, it’s not actually possible to give on universally correct answer.

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

Sidebar

Related Questions

I have this code :- using (System.Security.Cryptography.SHA256 sha2 = new System.Security.Cryptography.SHA256Managed()) { .. }
I have the following code. baseclass bc = new derivedclass(); Will the base class
I am new to programming.I have seen this code.returning a derived class object to
Let's say, I have this code of a jTree JTree jTree = new JTree()
I have this code in jQuery, that I want to reimplement with the prototype
I have this code: chars = #some list try: indx = chars.index(chars) except ValueError:
I have this code that performs an ajax call and loads the results into
I have this code #include <iostream> using namespace std; int main(int argc,char **argv) {
I have this code: CCalcArchive::CCalcArchive() : m_calcMap() { } m_calcMap is defined as this:
I have this code: myVariable which I want to change into trace(myVariable: + myVariable);

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.