Re: Garbage Collection and unreferenced local variable

Tech-Archive recommends: Speed Up your PC by fixing your registry



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.


.



Relevant Pages

  • Re: Garbage Collection and unreferenced local variable
    ... class TestClass ... public TestClass() ... TestClass instance x is created in the scope of the braces, ... references this instance and call the finalizer on the instance. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Scope of IEnumerable object within foreach
    ... > culprit turned out to be a finalizer on the class, ... > scope and so should it have done? ... > So although technically 'col' is in scope for the lifetime of the loop, ... If a garbage collection ...
    (microsoft.public.dotnet.general)
  • Scope of IEnumerable object within foreach
    ... modifications occuring within the loop and there is not another thread ... culprit turned out to be a finalizer on the class, ... scope and so should it have done? ... // Populates our combobox. ...
    (microsoft.public.dotnet.general)
  • Re: Garbage Collection and unreferenced local variable
    ... that the GC will take any notice of the braces. ... class TestClass ... public TestClass() ... references this instance and call the finalizer on the instance. ...
    (microsoft.public.dotnet.languages.csharp)
  • Garbage Collection and unreferenced local variable
    ... namespace ConsoleApplication1 ... class TestClass ... public TestClass() ... references this instance and call the finalizer on the instance. ...
    (microsoft.public.dotnet.languages.csharp)