Solved

Thread garbage collection

Posted on 2004-10-27
200 Views
Last Modified: 2010-04-15
Hi Y'all,

I'm sure this is an easy question for C# thread gurus but please enlighten a recent C++/MFC transplant.

I was just reading about timers in C# and I came across the following snippet in MSDN.

<MSDN>
Note   As long as you are using a Timer, you must keep a reference to it. As with any managed object, a Timer is subject to garbage collection when there are no references to it. The fact that a Timer is still active does not prevent it from being collected.
</MSDN>

This got me thinking about my current design in which I utilize threads fairly heavily.   Does this apply to threads also?  I have not been keeping references to the threads I create, but it hasn't seemed to cause a problem yet.  

Does this mean they could just be removed by garbage collector even though they are in the process of doing work??




<Example Code>

public void StartServerThread()
{
    ServerThread s = new ServerThread();
    Thread t = new Thread(new ThreadStart(s.ThreadProcReceive));
    t.IsBackground = true;
    t.Start();
    // will thread t eventually be collected once I exit this function?
}

0
Question by:AaronReams
    5 Comments
     
    LVL 8

    Expert Comment

    by:Razzie_
    Hi AaronReams,

    When your thread is not yet terminated, it won't be garbage collected.

    HTH,

    Razzie
    0
     
    LVL 8

    Author Comment

    by:AaronReams
    Hi Razzie,

    I just wrote a test application which starts 200 threads and then allocates and deallocates a bunch of memory.  It seems you are correct that the garbage collector doesn't clean up threads although there is no reference to the thread handle.  This is pretty much what I expected but I just wanted to double check so it didn't cause a problem in the future.

    That quote from MSDN I mentioned just makes it sound like it may be collected at some point because its a managed object with no reference to it, but perhaps .NET doesn't consider a thread a managed object.

    "As with any managed object, a Timer is subject to garbage collection when there are no references to it."

    Cheers,
    Aaron
    0
     
    LVL 8

    Expert Comment

    by:Razzie_
    Hmm to be honest I don't have a good answer to it as of now. I'm quite sure a thread does indeed have a reference to it somewhere, but how and where I don't know. I suppose I'll try to get a good answer to that, I'm curious now myself :)
    0
     
    LVL 8

    Accepted Solution

    by:
    Ok hopefully a better answer. This comes from Willy Denoyette (MVP), though somewhat in my own words:

    In short, when you start a thread, the CLR holds a reference count for the native thread (which is wrapped by the managed thread) in the ThreadStore. When the thread finishes, the reference count is decremented and it has no more references, and thus a thread will then be garbage collected.

    --------------------

    So that explains why a running thread is not garbage collected, like I suspected in my previous post, there are still references to it.
    I hope this clears things up a little bit.

    Cheers,
    Razzie
    0
     
    LVL 8

    Author Comment

    by:AaronReams
    Kewl.  Thanks for the break down.  I knew there had to be a reference somewhere.

    Cheers mate,
    Aaron
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
    Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
    Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    846 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now