Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Thread garbage collection

Posted on 2004-10-27
5
Medium Priority
?
203 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
Comment
Question by:AaronReams
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 8

Expert Comment

by:Razzie_
ID: 12428306
Hi AaronReams,

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

HTH,

Razzie
0
 
LVL 8

Author Comment

by:AaronReams
ID: 12478600
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_
ID: 12479026
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:
Razzie_ earned 500 total points
ID: 12487153
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
ID: 12487543
Kewl.  Thanks for the break down.  I knew there had to be a reference somewhere.

Cheers mate,
Aaron
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

609 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