threads and garbage collection

  • Thread starter Thread starter Sunny
  • Start date Start date
S

Sunny

Hi all,

Does any one knows, if I create a thread in a method and start some
processing in it, what happens when this thread object goes out of
scope. Is it garbage collected (I assume yes, as it is a regular object
and there is no reference to it). If it is, what happens with the
executed code inside the thread? What kind of exceptions can I receive
(if any) and how to capture them (AppDomain.UnhandledException may help,
but how do I recognize that this is in the collected thread?).

This is a sample:

using System;
using System.Threading;

namespace Test {

public class Test {

static ManualResetEvent evnt = new ManualResetEvent(false);

static void CreateThread()
{
Thread t = new Thread(new ThreadStart(ThreadMethod));
t.IsBackground = true;
t.Start();
}

static void ThreadMethod()
{
Console.WriteLine("Thread started");
evnt.Set();
while (true)
{
Console.WriteLine("From thread.");
Thread.Sleep(1000);
}
}

[STAThread]
public static void Main()
{
CreateThread();
evnt.WaitOne();
Console.WriteLine("press enter to GC");
Console.ReadLine();
GC.Collect();
GC.WaitForPendingFinalizers();
Console.WriteLine("press enter for exit");
Console.ReadLine();
}
}
}


The thread continues to run even after GC.Collect? Why? What holds a
reference?

And why TaskManager shows 6 threads running (before and after the
GC.Collect call) and 5 after that? I can count to 3 - the main one, GC
thread and my worker thread. What are the others?

Thanks
Sunny
 
Sunny said:
Hi all,

Does any one knows, if I create a thread in a method and start some
processing in it, what happens when this thread object goes out of
scope.

Nothing in particular.
Is it garbage collected (I assume yes, as it is a regular object
and there is no reference to it).

No. The CLR (in effect, though perhaps not literally) keeps a pointer to
each active thread. Until the thread exits, it will not be collected.
 
Hi Sunny

Threads are considered GC Roots by the runtime, so they are not collected (otherwise you'd have to have a strong reference to every running thread).

Hope that helps
-Chris

--------------------
Hi all,

Does any one knows, if I create a thread in a method and start some
processing in it, what happens when this thread object goes out of
scope. Is it garbage collected (I assume yes, as it is a regular object
and there is no reference to it). If it is, what happens with the
executed code inside the thread? What kind of exceptions can I receive
(if any) and how to capture them (AppDomain.UnhandledException may help,
but how do I recognize that this is in the collected thread?).

This is a sample:

using System;
using System.Threading;

namespace Test {

public class Test {

static ManualResetEvent evnt = new ManualResetEvent(false);

static void CreateThread()
{
Thread t = new Thread(new ThreadStart(ThreadMethod));
t.IsBackground = true;
t.Start();
}

static void ThreadMethod()
{
Console.WriteLine("Thread started");
evnt.Set();
while (true)
{
Console.WriteLine("From thread.");
Thread.Sleep(1000);
}
}

[STAThread]
public static void Main()
{
CreateThread();
evnt.WaitOne();
Console.WriteLine("press enter to GC");
Console.ReadLine();
GC.Collect();
GC.WaitForPendingFinalizers();
Console.WriteLine("press enter for exit");
Console.ReadLine();
}
}
}


The thread continues to run even after GC.Collect? Why? What holds a
reference?

And why TaskManager shows 6 threads running (before and after the
GC.Collect call) and 5 after that? I can count to 3 - the main one, GC
thread and my worker thread. What are the others?

Thanks
Sunny


--

This posting is provided "AS IS" with no warranties, and confers no rights. Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

Note: For the benefit of the community-at-large, all responses to this message are best directed to the newsgroup/thread from which they originated.
 
Thanks Mike,
exactly what I see, but I was not sure if this is the expected
behaviour.

Sunny
 
Thanks Chris,

can you provide a link for more info, it is interesting subject :). I.e.
I'm interested when they are considered collectable, etc.

Sunny


Hi Sunny

Threads are considered GC Roots by the runtime, so they are not collected (otherwise you'd have to have a strong reference to every running thread).

Hope that helps
-Chris

--------------------
Hi all,

Does any one knows, if I create a thread in a method and start some
processing in it, what happens when this thread object goes out of
scope. Is it garbage collected (I assume yes, as it is a regular object
and there is no reference to it). If it is, what happens with the
executed code inside the thread? What kind of exceptions can I receive
(if any) and how to capture them (AppDomain.UnhandledException may help,
but how do I recognize that this is in the collected thread?).

This is a sample:

using System;
using System.Threading;

namespace Test {

public class Test {

static ManualResetEvent evnt = new ManualResetEvent(false);

static void CreateThread()
{
Thread t = new Thread(new ThreadStart(ThreadMethod));
t.IsBackground = true;
t.Start();
}

static void ThreadMethod()
{
Console.WriteLine("Thread started");
evnt.Set();
while (true)
{
Console.WriteLine("From thread.");
Thread.Sleep(1000);
}
}

[STAThread]
public static void Main()
{
CreateThread();
evnt.WaitOne();
Console.WriteLine("press enter to GC");
Console.ReadLine();
GC.Collect();
GC.WaitForPendingFinalizers();
Console.WriteLine("press enter for exit");
Console.ReadLine();
}
}
}


The thread continues to run even after GC.Collect? Why? What holds a
reference?

And why TaskManager shows 6 threads running (before and after the
GC.Collect call) and 5 after that? I can count to 3 - the main one, GC
thread and my worker thread. What are the others?

Thanks
Sunny
 
Hi Sunny

As long as the thread is running, it will not be collected. This is because a running thread is an implicit GC root, with a thread of execution. Without that root, your thread couldn't
run to completion if it went out of scope. Take a look in MSDN for articles on threading and GC for more information.

-Chris

--------------------
From: Sunny <[email protected]>
Subject: RE: threads and garbage collection
Date: Tue, 27 Jul 2004 17:48:56 -0500
References: <[email protected]> <[email protected]>
Organization: Iceberg Wireless LLC
MIME-Version: 1.0
Content-Type: text/plain; charset="iso-8859-15"
Content-Transfer-Encoding: 7bit
X-Newsreader: MicroPlanet Gravity v2.60
Message-ID: <[email protected]>
Newsgroups: microsoft.public.dotnet.languages.csharp
NNTP-Posting-Host: 216.17.90.91
Lines: 1
Path: cpmsftngxa10.phx.gbl!TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.phx.gbl
Xref: cpmsftngxa10.phx.gbl microsoft.public.dotnet.languages.csharp:261350
X-Tomcat-NG: microsoft.public.dotnet.languages.csharp

Thanks Chris,

can you provide a link for more info, it is interesting subject :). I.e.
I'm interested when they are considered collectable, etc.

Sunny


Hi Sunny

Threads are considered GC Roots by the runtime, so they are not collected (otherwise you'd have to have a strong reference to every running thread).

Hope that helps
-Chris

--------------------
Hi all,

Does any one knows, if I create a thread in a method and start some
processing in it, what happens when this thread object goes out of
scope. Is it garbage collected (I assume yes, as it is a regular object
and there is no reference to it). If it is, what happens with the
executed code inside the thread? What kind of exceptions can I receive
(if any) and how to capture them (AppDomain.UnhandledException may help,
but how do I recognize that this is in the collected thread?).

This is a sample:

using System;
using System.Threading;

namespace Test {

public class Test {

static ManualResetEvent evnt = new ManualResetEvent(false);

static void CreateThread()
{
Thread t = new Thread(new ThreadStart(ThreadMethod));
t.IsBackground = true;
t.Start();
}

static void ThreadMethod()
{
Console.WriteLine("Thread started");
evnt.Set();
while (true)
{
Console.WriteLine("From thread.");
Thread.Sleep(1000);
}
}

[STAThread]
public static void Main()
{
CreateThread();
evnt.WaitOne();
Console.WriteLine("press enter to GC");
Console.ReadLine();
GC.Collect();
GC.WaitForPendingFinalizers();
Console.WriteLine("press enter for exit");
Console.ReadLine();
}
}
}


The thread continues to run even after GC.Collect? Why? What holds a
reference?

And why TaskManager shows 6 threads running (before and after the
GC.Collect call) and 5 after that? I can count to 3 - the main one, GC
thread and my worker thread. What are the others?

Thanks
Sunny


--

This posting is provided "AS IS" with no warranties, and confers no rights. Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

Note: For the benefit of the community-at-large, all responses to this message are best directed to the newsgroup/thread from which they originated.
 
Back
Top