473,624 Members | 2,539 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

threads and garbage collection

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.Unha ndledException may help,
but how do I recognize that this is in the collected thread?).

This is a sample:

using System;
using System.Threadin g;

namespace Test {

public class Test {

static ManualResetEven t evnt = new ManualResetEven t(false);

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

static void ThreadMethod()
{
Console.WriteLi ne("Thread started");
evnt.Set();
while (true)
{
Console.WriteLi ne("From thread.");
Thread.Sleep(10 00);
}
}

[STAThread]
public static void Main()
{
CreateThread();
evnt.WaitOne();
Console.WriteLi ne("press enter to GC");
Console.ReadLin e();
GC.Collect();
GC.WaitForPendi ngFinalizers();
Console.WriteLi ne("press enter for exit");
Console.ReadLin e();
}
}
}
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
Nov 16 '05 #1
5 2251

"Sunny" <su***@newsgrou ps.nospam> wrote in message
news:e7******** ******@tk2msftn gp13.phx.gbl...
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.
Nov 16 '05 #2
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.Unha ndledException may help,
but how do I recognize that this is in the collected thread?).

This is a sample:

using System;
using System.Threadin g;

namespace Test {

public class Test {

static ManualResetEven t evnt = new ManualResetEven t(false);

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

static void ThreadMethod()
{
Console.WriteLi ne("Thread started");
evnt.Set();
while (true)
{
Console.WriteLi ne("From thread.");
Thread.Sleep(10 00);
}
}

[STAThread]
public static void Main()
{
CreateThread();
evnt.WaitOne();
Console.WriteLi ne("press enter to GC");
Console.ReadLin e();
GC.Collect();
GC.WaitForPendi ngFinalizers();
Console.WriteLi ne("press enter for exit");
Console.ReadLin e();
}
}
}
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.

Nov 16 '05 #3
Thanks Mike,
exactly what I see, but I was not sure if this is the expected
behaviour.

Sunny

In article <er************ **@TK2MSFTNGP10 .phx.gbl>,
ms************* @hotmail.com says...

"Sunny" <su***@newsgrou ps.nospam> wrote in message
news:e7******** ******@tk2msftn gp13.phx.gbl...
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.

Nov 16 '05 #4
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
In article <9s************ **@cpmsftngxa10 .phx.gbl>,
cl***@online.mi crosoft.com says...
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.Unha ndledException may help,
but how do I recognize that this is in the collected thread?).

This is a sample:

using System;
using System.Threadin g;

namespace Test {

public class Test {

static ManualResetEven t evnt = new ManualResetEven t(false);

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

static void ThreadMethod()
{
Console.WriteLi ne("Thread started");
evnt.Set();
while (true)
{
Console.WriteLi ne("From thread.");
Thread.Sleep(10 00);
}
}

[STAThread]
public static void Main()
{
CreateThread();
evnt.WaitOne();
Console.WriteLi ne("press enter to GC");
Console.ReadLin e();
GC.Collect();
GC.WaitForPendi ngFinalizers();
Console.WriteLi ne("press enter for exit");
Console.ReadLin e();
}
}
}
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


Nov 16 '05 #5
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 <su***@newsgrou ps.nospam>
Subject: RE: threads and garbage collection
Date: Tue, 27 Jul 2004 17:48:56 -0500
References: <e7************ **@tk2msftngp13 .phx.gbl> <9s************ **@cpmsftngxa10 .phx.gbl>
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: <uV************ *@tk2msftngp13. phx.gbl>
Newsgroups: microsoft.publi c.dotnet.langua ges.csharp
NNTP-Posting-Host: 216.17.90.91
Lines: 1
Path: cpmsftngxa10.ph x.gbl!TK2MSFTNG XA01.phx.gbl!TK 2MSFTNGP08.phx. gbl!tk2msftngp1 3.phx.gbl
Xref: cpmsftngxa10.ph x.gbl microsoft.publi c.dotnet.langua ges.csharp:2613 50
X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.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
In article <9s************ **@cpmsftngxa10 .phx.gbl>,
cl***@online.m icrosoft.com says...
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.Unha ndledException may help,
>but how do I recognize that this is in the collected thread?).
>
>This is a sample:
>
>using System;
>using System.Threadin g;
>
>namespace Test {
>
>public class Test {
>
> static ManualResetEven t evnt = new ManualResetEven t(false);
>
> static void CreateThread()
> {
> Thread t = new Thread(new ThreadStart(Thr eadMethod));
> t.IsBackground = true;
> t.Start();
> }
>
> static void ThreadMethod()
> {
> Console.WriteLi ne("Thread started");
> evnt.Set();
> while (true)
> {
> Console.WriteLi ne("From thread.");
> Thread.Sleep(10 00);
> }
> }
>
> [STAThread]
> public static void Main()
> {
> CreateThread();
> evnt.WaitOne();
> Console.WriteLi ne("press enter to GC");
> Console.ReadLin e();
> GC.Collect();
> GC.WaitForPendi ngFinalizers();
> Console.WriteLi ne("press enter for exit");
> Console.ReadLin e();
> }
>}
>}
>
>
>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.

Nov 16 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2324
by: Thomas Smith | last post by:
I am building a binding to a runtime engine that was written in C++. I have no problems setting up the mapping from object calls to the native implementation, and storing reference to those objects. The problem comes when I start a new thread... The new thread does not have access to the same memory, and I end up with a pointer to dead space. Has anyone out there run in to this problem and have an answer? Actually, it doesn't matter...
6
810
by: Ganesh | last post by:
Is there a utility by microsoft (or anyone) to force garbage collection in a process without have access to the process code. regards Ganesh
11
2720
by: Rick | last post by:
Hi, My question is.. if Lisp, a 40 year old language supports garbage collection, why didn't the authors of C++ choose garbage collection for this language? Are there fundamental reasons behind this? Is it because C is generally a 'low level' language and they didn't want garbage collection to creep into C++ and ruin everything? Just wondering :)
34
6403
by: Ville Voipio | last post by:
I would need to make some high-reliability software running on Linux in an embedded system. Performance (or lack of it) is not an issue, reliability is. The piece of software is rather simple, probably a few hundred lines of code in Python. There is a need to interact with network using the socket module, and then probably a need to do something hardware- related which will get its own driver written in C.
5
3589
by: Bob lazarchik | last post by:
Hello: We are considering developing a time critical system in C#. Our tool used in Semiconductor production and we need to be able to take meaurements at precise 10.0 ms intervals( 1000 measurement exactly 10 ms apart. In the future this may decrease to 5ms ). I am concerned that if garbage collection invokes during this time it may interfere with our measurement results. I have looked over the garbage collection mechanism and see no...
8
12915
by: Martin Maat | last post by:
I am puzzled. I have this object that uses a thread. The thread is encapsulated by the object, the object has Start and Stop methods to enable the client to start or stop the thread. I found that the object will not be garbage collected while the thread is running. Fair enough, the documented explanation is that the GC compresses objects on the heap and needs to update references, the references will be invalid for a couple of moments...
8
3032
by: mike2036 | last post by:
For some reason it appears that garbage collection is releasing an object that I'm still using. The object is declared in a module and instantiated within a class that is in turn instantiated by the mainline. The class that instantiated the object in question is definitely still in existence at the point garbage collection swoops in and yanks it out from under my processing. Is there a way to ensure an instantiated object cannot be freed...
56
3655
by: Johnny E. Jensen | last post by:
Hellow I'am not sure what to think about the Garbage Collector. I have a Class OutlookObject, It have two private variables. Private Microsoft.Office.Interop.Outlook.Application _Application = null; Private Microsoft.Office.Interop.Outlook.NameSpace _Namespace = null; The Constructor: public OutlookObject()
158
7786
by: pushpakulkar | last post by:
Hi all, Is garbage collection possible in C++. It doesn't come as part of language support. Is there any specific reason for the same due to the way the language is designed. Or it is discouraged due to some specific reason. If someone can give inputs on the same, it will be of great help. Regards, Pushpa
0
8246
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8631
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7174
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6112
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5570
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4084
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4184
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1796
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1489
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.