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.

<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?
}

LVL 8
AaronReamsAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Razzie_Connect With a Mentor Commented:
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
 
Razzie_Commented:
Hi AaronReams,

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

HTH,

Razzie
0
 
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."

Cheers,
Aaron
0
 
Razzie_Commented:
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
 
AaronReamsAuthor Commented:
Kewl.  Thanks for the break down.  I knew there had to be a reference somewhere.

Cheers mate,
Aaron
0
All Courses

From novice to tech pro — start learning today.