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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T03:30:57+00:00 2026-05-19T03:30:57+00:00

Folks, If I set a large object to .net to null in the middle

  • 0

Folks,

If I set a large object to .net to null in the middle of a long-running method (not necessarily CPU intensive…just long-running) is it immediately game for garbage collection OR does the method need to complete before the object is ready for garbage collection?

  • 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-19T03:30:57+00:00Added an answer on May 19, 2026 at 3:30 am

    The method doesn’t need to complete, but neither do you need to set the variable to null, if the GC can tell you’re not going to read from it again. For example:

    public void Foo()
    {
        SomeObject x = new SomeObject();
        // Code which uses x
    
        Console.WriteLine("Eligible for collection");
    
        // Code which doesn't use x.
    }
    

    The object is eligible for collection at the indicated point – assuming nothing else has kept a reference to it, of course. The important thing is whether anything is ever going to be able to read that value again. You can even assign the variable a different value and then read it, and so long as the GC knows that it won’t see the original value again, that won’t act as a GC root. For example:

    using System;
    
    class Bomb
    {
        readonly string name;
    
        public Bomb(string name)
        {
            this.name = name;
        }
    
        ~Bomb()
        {
            Console.WriteLine(name + " - going boom!");
        }    
    
        public override string ToString()
        {
            return name;
        }
    }
    
    class Test
    {
        static void Main()
        {
            Bomb b = new Bomb("First bomb");
            Console.WriteLine("Using bomb...");
            Console.WriteLine(b);
            Console.WriteLine("Not using it any more");
    
            GC.Collect();
            GC.WaitForPendingFinalizers();
    
            Console.WriteLine("Creating second bomb...");
            b = new Bomb("Second bomb");
            Console.WriteLine("Using second bomb...");
            Console.WriteLine(b);
            Console.WriteLine("End of main");
        }
    }
    

    Output:

    Using bomb...
    First bomb
    Not using it any more
    First bomb - going boom!
    Creating second bomb...
    Using second bomb...
    Second bomb
    End of main
    Second bomb - going boom!
    

    In fact, it can get more extreme than that: an object can be eligible for garbage collection even while a method is running “in” it so long as the GC can detect that nothing can ever read a field again. Here’s a short but complete example:

    using System;
    
    class Bomb
    {
        int x = 10;
    
        ~Bomb()
        {
            Console.WriteLine("Boom!");
        }
    
        public void GoBang()
        {
            Console.WriteLine("Start of GoBang");
            GC.Collect();
            GC.WaitForPendingFinalizers();
    
            Console.WriteLine("x={0}", x);
            Console.WriteLine("No more reads of x");
            GC.Collect();
            GC.WaitForPendingFinalizers();
    
            Console.WriteLine("Returning");
        }
    }
    
    class Test
    {
        static void Main()
        {
            Bomb b = new Bomb();
            b.GoBang();
            Console.WriteLine("Still in Main");
        }
    }
    

    Output:

    Start of GoBang
    x=10
    No more reads of x
    Boom!
    Returning
    Still in Main
    

    (Run this not in the debugger – the debugger delays garbage collection so that you can still watch variables.)

    One point to note: your question talks about setting an object to null… that concept doesn’t exist. You only ever set a variable to null. It’s worth distinguishing between the two.

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

Sidebar

Related Questions

Folks, I need to maintain a C#/.Net desktop application. So, I need to set
Folks, I've a large legacy .Net code base, and I'm trying to introduce Unit
Folks, I connect to a large number of SQL Server 2005 databases through SQL
I set django's settings.py file to chmod 600 to keep felonious folks from spying
Hey folks. I have a client who's old website was called toastkid.com. I set
UPDATE: This is a Django web app Hi folks, I want to set up
Hi folks I'm trying to set up tagging in my CakePHP application. I've been
Hey folks, diving into jQuery and loving it. I have a set of links
Folks, Given a set of sorted values (perhaps in a List<T>, SortedList<T,K>, etc.) what's
Folks, I've got my sample application for WP7 running. I've also got a service

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.