G
Guest
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.
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.