Garbage Collection

  • Thread starter Thread starter C# Learner
  • Start date Start date
C

C# Learner

I keep seeing code like this in tutorials:

public void Start()
{
Thread thread = new Thread(new ThreadStart(Callback));
thread.IsBackground = true;
thread.Start();
}

This seems erroneous to me. Surely thread is then subject to garbage
collection, since it has a reference count of 0 after execution leaves
this method.

Am I missing something here?
 
C# Learner said:
I keep seeing code like this in tutorials:

public void Start()
{
Thread thread = new Thread(new ThreadStart(Callback));
thread.IsBackground = true;
thread.Start();
}

This seems erroneous to me. Surely thread is then subject to garbage
collection, since it has a reference count of 0 after execution leaves
this method.

Am I missing something here?

Yes - a thread which has been started has a reference to itself,
effectively. Otherwise what's keeping even the current thread alive?
 
Hi C# Learner

The CLR Garbage Collector doesn't use reference counting, rather a concept of "GC roots". Roots are references that are not garbage (variables in scope, statics, etc), and
so will not be collected. One of the properties of threads is that they are roots.

Hope that helps
-Chris

--------------------
From: C# Learner <[email protected]>
Subject: Garbage Collection
Date: Tue, 03 Feb 2004 10:58:24 +0000
Message-ID: <[email protected]>
X-Newsreader: Forte Agent 1.93/32.576 English (American)
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Newsgroups: microsoft.public.dotnet.languages.csharp
NNTP-Posting-Host: user-4279.l3.c1.dsl.pol.co.uk 81.77.16.183
Lines: 1
Path: cpmsftngxa07.phx.gbl!cpmsftngxa10.phx.gbl!TK2MSFTNGXA05.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP12.phx.gbl
Xref: cpmsftngxa07.phx.gbl microsoft.public.dotnet.languages.csharp:217698
X-Tomcat-NG: microsoft.public.dotnet.languages.csharp

I keep seeing code like this in tutorials:

public void Start()
{
Thread thread = new Thread(new ThreadStart(Callback));
thread.IsBackground = true;
thread.Start();
}

This seems erroneous to me. Surely thread is then subject to garbage
collection, since it has a reference count of 0 after execution leaves
this method.

Am I missing something here?


--

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.
 
The best explanation of GC ever, bar none, can be found in Jeff Richter's
classic "Applied Microsoft .NET Framework Programming" Microsoft Press.

--
--------------------------------------------------------------
Sam Gentile [C#/.NET MVP]
..NET Blog http://samgentile.com/blog/
MSDN Column:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/html/bridge.asp
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top