Re: Garbage Collection and unreferenced local variable
- From: dotnetismylife <dotnetismylife@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Tue, 28 Nov 2006 02:24:02 -0800
Hi DeveloperX,
once the object goes out of scope and you forcefully call the GC on all
generations the finalizer should be called (if the object has one) since my
understanding is that the GC will kick off the finalize queue thread, and
the finalizer is deterministically being called in the second code sample
when the variable is explicitly set to null, so I am not sure why I have to
set it to null, when it is already out of scope in terms if the {}.
"DeveloperX" wrote:
My understanding is that you can't make it finalize anything. You can.
invoke the GC, but all that does is ask it to see if it thinks there's
a need to recover used memory. The nearest you can get is to use
IDispose, which still won't clean up, but does give you a sort of
determanistic way to tidy up as the object goes out of scope. It
doesn't happen automatically of course, you have to do it manually, or
use the object in a using block.
dotnetismylife wrote:
Hi all,
I have a question, in the following code:
using System;
using System.Diagnostics;
namespace ConsoleApplication1
{
class TestClass
{
public TestClass()
{
Debug.WriteLine("TestClass constructor");
}
~TestClass()
{
Debug.WriteLine("TestClass finalizer");
}
}
class Program
{
static void Main(string[] args)
{
{
TestClass x = new TestClass();
//Keep compiler honest
Debug.WriteLine(x.ToString());
}
GC.Collect();
GC.WaitForPendingFinalizers();
}
}
}
TestClass instance x is created in the scope of the {} braces, so after the
closing brace x goes out of scope and it is not possible to reference it any
more. I would think that x should not be considered to be a root for garbage
collection, so when GC.Collect is called the GC would see that nothing
references this instance and call the finalizer on the instance. However
this does not happen, the finalizer is not called, however if I put an
explicit assignment of null to x i.e. "x=null;" inside the {} then it's
finalizer is called.
So my question is, are the {} not considered to dictate garbage collection,
I would hope so, but it does not seem the case, below is the code that does
call the finalizer:
using System;
using System.Diagnostics;
namespace ConsoleApplication1
{
class TestClass
{
public TestClass()
{
Debug.WriteLine("TestClass constructor");
}
~TestClass()
{
Debug.WriteLine("TestClass finalizer");
}
}
class Program
{
static void Main(string[] args)
{
{
TestClass x = new TestClass();
//Keep compiler honest
Debug.WriteLine(x.ToString());
x = null;
}
GC.Collect();
GC.WaitForPendingFinalizers();
}
}
}
Thanks
dniml.
- Follow-Ups:
- Re: Garbage Collection and unreferenced local variable
- From: DeveloperX
- Re: Garbage Collection and unreferenced local variable
- References:
- Garbage Collection and unreferenced local variable
- From: dotnetismylife
- Re: Garbage Collection and unreferenced local variable
- From: DeveloperX
- Garbage Collection and unreferenced local variable
- Prev by Date: Re: How can i escape special characters( % [ _ ) in an Access SQL statement?
- Next by Date: Re: Garbage Collection and unreferenced local variable
- Previous by thread: Re: Garbage Collection and unreferenced local variable
- Next by thread: Re: Garbage Collection and unreferenced local variable
- Index(es):
Relevant Pages
|