
Signature
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.
>> 1. GC will not recollect the memory assigned to running object.
>
[quoted text clipped - 40 lines]
> Called GC
> End of method
> 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.
Well, until there are no more pending references which might be
used.The JIT is able to detect when the currently executing no longer
actually uses its implicit "this" reference, which is why it can give
the output I showed before.
> Executing the example
> given by Jon below on my machine gave the following output:
[quoted text clipped - 3 lines]
> End of method
> Finalizer
How were you running it? If you run it in the debugger, you'll always
see the above.
> To better illustrate this point, see the attached file that contains a
> modified version of the example below. As expected, the output is:
[quoted text clipped - 4 lines]
> End of method
> Finalizer of 2
It may be for you, but compiling with csc Test.cs /o+ /debug-
and then running I got:
Start of method
Finalizer of 2
Finalizer of 1
Called GC
End of method
Again, it will depend on how you're running the code. I always run this
kind of thing from the command line, to closer mirror production
situations (compared with running from Visual Studio, where you have to
be careful not to end up using the debugger).
Just to give a bit more evidence that I'm not making this stuff up, see
this blog entry by Chris Brumme:
http://blogs.msdn.com/cbrumme/archive/2003/04/19/51365.aspx

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Laura T. - 21 May 2007 15:59 GMT
>> If an object has a reference available from an application root, it
>> will not be collected. In the example below, when Invoke is being
[quoted text clipped - 6 lines]
> actually uses its implicit "this" reference, which is why it can give
> the output I showed before.
So we can safely say that there is no "Premature garbage collection" as the
OP's title states.
>> Executing the example
>> given by Jon below on my machine gave the following output:
[quoted text clipped - 34 lines]
>
> http://blogs.msdn.com/cbrumme/archive/2003/04/19/51365.aspx
Jon Skeet [C# MVP] - 21 May 2007 18:04 GMT
> > Well, until there are no more pending references which might be
> > used.The JIT is able to detect when the currently executing no longer
[quoted text clipped - 3 lines]
> So we can safely say that there is no "Premature garbage collection" as the
> OP's title states.
It's premature compared with the OP's *expectation* which is pretty
important, IMO.

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Barry Kelly - 22 May 2007 05:44 GMT
> > Well, until there are no more pending references which might be
> > used.The JIT is able to detect when the currently executing no longer
[quoted text clipped - 3 lines]
> So we can safely say that there is no "Premature garbage collection" as the
> OP's title states.
It's still very important if you have expectations of when the finalizer
will be executed or when weak references will be null'd - and they're
pretty much the only observable side-effects of garbage collection.
If the OP has a reason to wonder about when GC happens, and when an
object is collected, chances are that he needs to know the details here.
-- Barry

Signature
http://barrkel.blogspot.com/