Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will .Net Garbage Collect an object that's not referenced, but has a thread that's doing work?

I have the following code (cut down for readability):

Main Class:

public StartProcess()
{
    Thinker th = new Thinker();
    th.DoneThinking += new Thinker.ProcessingFinished(ThinkerFinished);
    th.StartThinking();
}

void ThinkerFinished()
{
    Console.WriteLine("Thinker finished");
}

Thinker Class:

public class Thinker
{
    private System.Timers.Timer t;

    public delegate void ProcessingFinished();
    public event ProcessingFinished DoneThinking;

    BackgroundWorker backgroundThread;

    public Thinker() { }

    public StartThinking()
    {
        t = new System.Timers.Timer(5000);    // 5 second timer
        t.AutoReset = false;
        t.Elapsed += new System.Timers.ElapsedEventHandler(t_Elapsed);
        t.Start();

        // start a background thread to do the thinking
        backgroundThread = new BackgroundWorker();
        backgroundThread.DoWork += new DoWorkEventHandler(BgThread_DoWork);
        backgroundThread.RunWorkerAsync();
    }

    void t_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
        DoneThinking();
    }

    BgThread_DoWork(object sender, DoWorkEventArgs e)
    {
        // work in here should go for much less than 5 seconds
        // it will die if it doesn't

        t.Stop();
        DoneThinking();
    }
}

What I originally expected to happen was that the event handler in the main class would prevent the Thinker from being garbage collected.

Apparently this isn't the case.

I'm now wondering whether garbage collection will occur regardless of whether this thread is "busy" or not. In other words, is there a chance it will be garbage collected before the 5 second timeout has expired?

To put it another way, is it possible for the garbage collector to collect my Thinker before it's finished processing?

like image 831
Damovisa Avatar asked Jun 24 '09 04:06

Damovisa


People also ask

Can a thread object be collected by the garbage collector while running?

No thread (or the things it refers to) will be garbage-collected while it is still running.

What happens to an execution thread when garbage collection runs?

run() will finish its execution, your current thread will sleep, and your while will continue its loop and do it over and over. The GC can hit at any point and, as others have stated, running threads (threads that were started with start() ) will only be GCed if they have finished their execution.

How does the .NET garbage collector work?

NET's garbage collector manages the allocation and release of memory for your application. Each time you create a new object, the common language runtime allocates memory for the object from the managed heap.

Is garbage collector a thread?

Java Garbage Collector runs as a Daemon Thread (i.e. a low priority thread that runs in the background to provide services to user threads or perform JVM tasks).


2 Answers

No, a thread is considered live as long as it is referenced, and any thread that is running is considered to be referenced (IIRC a running thread registers its stack as a GC root, and that stack will reference the thread).

That said i'm looking at your example and i don't understand where you believe a thread is being spawned?

like image 60
olliej Avatar answered Sep 27 '22 17:09

olliej


No, a running thread's stack acts as a root for GC purposes. That stack will live as long as the Thread is running, so the Thread itself won't be collected as long its running.

Here's an article that mentions (among other things) what the roots are for GC purposes. To save some time, GC roots are global objects, static objects, all reference on all thread stacks, and all CPU registers containing references.

like image 31
Kevin Montrose Avatar answered Sep 27 '22 17:09

Kevin Montrose