Thread garbage collection

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.

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.

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;
    // will thread t eventually be collected once I exit this function?

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Hi AaronReams,

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


AaronReamsAuthor Commented:
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."

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 :)
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.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AaronReamsAuthor Commented:
Kewl.  Thanks for the break down.  I knew there had to be a reference somewhere.

Cheers mate,
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.