Re: Prematurely garbage collection



If an object has a reference available from an application root, it will not be collected. In the example below, when Invoke is being executed, the reference of the instance is available with app root - so the object is not collected until there are no more pending references. Executing the example given by Jon below on my machine gave the following output:

Start of method
Called GC
End of method
Finalizer

To better illustrate this point, see the attached file that contains a modified version of the example below. As expected, the output is:

Start of method
Finalizer of 1
Called GC
End of method
Finalizer of 2

--

Thanks!
Gaurav
WinToolZone - http://www.wintoolzone.com/
Inside and Out - http://www.wintoolzone.com/blog/
The information in this post is provided "AS IS" with no warranties, and confers no rights.


"Jon Skeet [C# MVP]" <skeet@xxxxxxxxx> wrote in message news:MPG.20b8326cc9c8494d128@xxxxxxxxxxxxxxxxxxxxxxx
Chitrsen <Chitrsen@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
1. GC will not recollect the memory assigned to running object.

Yes it can.

2. if you open IL of code '(new Instance()).Invoke();', you can see clr
gives a random generated name for this instance. use ildasm.exe to verify it.

It can still be garbage collected before Invoke() has finished
executing. Here's an example:

using System;
using System.Threading;

class Test
{
~Test()
{
Console.WriteLine ("Finalizer");
}

void Invoke()
{
Console.WriteLine ("Start of method");
GC.Collect();
GC.WaitForPendingFinalizers();

Console.WriteLine ("Called GC");

Thread.Sleep(1000);
Console.WriteLine ("End of method");
}


static void Main()
{
new Test().Invoke();
}
}

On my box that prints:
Start of method
Finalizer
Called GC
End of method

--
Jon Skeet - <skeet@xxxxxxxxx>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
using System;
using System.Threading;

class Test
{
internal int m_index;

public Test(int index)
{
m_index = index;
}

~Test()
{
Console.WriteLine("Finalizer of {0}", m_index);
}

void Invoke()
{
Console.WriteLine("Start of method");
GC.Collect();
GC.WaitForPendingFinalizers();

Console.WriteLine("Called GC");

Thread.Sleep(1000);
Console.WriteLine("End of method");
}


static void Main()
{
new Test(1);
new Test(2).Invoke();
}
}


Relevant Pages

  • Re: CALL using OMITTED
    ... look for ways that Cobol can help with this. ... invoke MyCollection "new" returning CollectionObject ... I've never done it so I don't know the full implications - hopefully it creates a unique reference in the first Collection without impacting the second. ... Procedure Div using.... ...
    (comp.lang.cobol)
  • Re: thread.Abort()
    ... > as you hang onto the reference. ... > executing code in a finally block - this can be interrupted with unknown ... > target thread is alive but before the next line of code executes a context ... > thread to actually terminate is also unbounded. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: new block notation (was: Re: ruby-dev summary 26468-26661)
    ... It is possible to invoke all three in an arbitrary scope (where 'self' and instance variables refer to a specific, runtime-chosen instance of any class) by passing them as a block to a special ... It is possible to get a reference to a method and assign it to a variable. ... It is possible to invoke a method reference in the scope of the instance to which it is tied without supplying the instance as a receiver. ... Lambdas are not associated with any specific instance. ...
    (comp.lang.ruby)
  • Re: pasing parametersfor a function when a button is used
    ... You might want to read the syntax rules for Tcl. ... while executing ... ".b2 invoke" ... Many people that dislike Tcl ...
    (comp.lang.tcl)
  • Delphi Trojans
    ... I have a component I have been working on that happened to reference a unit ... the controller unit for an application which instantiates a controller ... noticed that delphi was executing this code, which was logging into a server ... No longer will I download components without source code. ...
    (borland.public.delphi.non-technical)