all groups > dotnet clr > may 2007 >
You're in the

dotnet clr

group:

Prematurely garbage collection


Prematurely garbage collection Amir Shitrit
5/16/2007 11:02:00 PM
dotnet clr: Hi.
In what cases is it possible that the GC will collect an object while a
member of that object is still running?
Could it happen when I create an object without referencing it and then
invoking one of its members?
For example: (new Instance()).Invoke(); // there's no reference to the
Re: Prematurely garbage collection Jon Skeet [C# MVP]
5/17/2007 12:00:00 AM
[quoted text, click to view]

If the JIT can prove that it won't be accessing any of its fields any
more.

[quoted text, click to view]

That entirely depends on what Invoke does. It has an implicit reference
to "this", but at the point where the JIT can spot that it's not going
to use any more fields (or call anything else that will use those
fields) the object is eligible for GC.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
Re: Prematurely garbage collection Laura T.
5/17/2007 1:26:19 PM

"Amir Shitrit" <AmirShitrit@discussions.microsoft.com> ha scritto nel
messaggio news:B080FE00-AB05-4A60-A836-6600892BAF9B@microsoft.com...
[quoted text, click to view]

Depends what you mean by "object is still running", but pratically it cannot
happen, in all managed code world.

[quoted text, click to view]

No, because it's not possible to invoke a member without a reference
(implicit or explicit) to the object.

[quoted text, click to view]

The (new Instance()) will be kept alive until Invoke() returns.
RE: Prematurely garbage collection Chitrsen
5/18/2007 2:49:07 AM

Hi,
1. GC will not recollect the memory assigned to running object.
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.

I hope this answers your question

Regards
Chitrsen

Re: Prematurely garbage collection Barry Kelly
5/18/2007 3:00:35 PM
[quoted text, click to view]

Actually, it can, if the remaining code path in methods of the instance
don't access the 'this' reference.

[quoted text, click to view]

This doesn't mean anything. The JIT needs to allocate a register or a
stack slot and store a reference where the GC can find it, in order to
prevent collection. If the code doesn't access the reference, the JIT
compiler can optimize the reference away. The IL is just input to the
JIT compiler.

[quoted text, click to view]

Be careful that your answers are correct!

Check out the following program (compile with 'csc test.cs' if saved to
test.cs):

---8<---
using System;

class C
{
~C()
{
Console.WriteLine("Collected!");
}

static void Main()
{
new C().Use();
}

void Use()
{
GC.Collect();
GC.WaitForPendingFinalizers();
Console.WriteLine("Used!");
}
}
--->8---

Run it and it results in:

---8<---
Collected!
Used!
--->8---

-- Barry

--
Re: Prematurely garbage collection Chris Mullins [MVP]
5/18/2007 3:11:22 PM
That's a pretty evil example.

I wouldn't have thought of it...

I'll have to save that one off as an Interview question one day. "What will
this print out?"

--
Chris Mullins, MCSD.NET, MCPD:Enterprise, Microsoft C# MVP
http://www.coversant.com/blogs/cmullins

[quoted text, click to view]

Re: Prematurely garbage collection Chris Mullins [MVP]
5/18/2007 4:55:00 PM
[quoted text, click to view]

I tend to ask questions like this to see:
1 - How does the candidate do out of their confort zone? Do they get
hostile? angry? confrontational? Or do they just think about it an go,
"Hmmm. Good question.".

2 - I like to see how people think. Critical thinking is just... key. An
answer of "I don't know, but I think the answer is xyz, for these
reasons..." is just fine with me, even if the answer is actually wrong.

Being somewhat of a jerk, I've also told candidates wong information just to
see what approach they would take to correct me.

If I'm going to be stuck working with someone for a while, I consider these
important things to know...

--
Chris Mullins, MCSD.NET, MCPD:Enterprise, Microsoft C# MVP
http://www.coversant.com/blogs/cmullins

RE: Prematurely garbage collection Jon Skeet [C# MVP]
5/18/2007 11:00:53 PM
[quoted text, click to view]

Yes it can.

[quoted text, click to view]

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@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
Re: Prematurely garbage collection Jon Skeet [C# MVP]
5/18/2007 11:53:55 PM
[quoted text, click to view]

The difference an empty static constructor makes is an equally evil
example:
http://www.yoda.arachsys.com/csharp/beforefieldinit.html
This blog entry has a few too:
http://msmvps.com/blogs/jon.skeet/archive/2005/10/02/68716.aspx


I wouldn't use these as interview questions to see whether the
candidate knows the answer. They are, however, quite good questions to
see how *interested* the candidate is in little bits of obscure
knowledge.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
Re: Prematurely garbage collection Amir Shitrit
5/19/2007 3:53:00 PM
Do you mean that altough the method has a pointer to "this" as a method
argument, the jitter can still unreference this argument?

[quoted text, click to view]
Re: Prematurely garbage collection Gaurav Khanna [MSFT]
5/20/2007 1:18:54 AM
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.


[quoted text, click to view]
Re: Prematurely garbage collection Jon Skeet [C# MVP]
5/20/2007 4:38:55 PM
[quoted text, click to view]

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.

[quoted text, click to view]

How were you running it? If you run it in the debugger, you'll always
see the above.

[quoted text, click to view]

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

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
Re: Prematurely garbage collection Laura T.
5/21/2007 4:59:23 PM

"Jon Skeet [C# MVP]" <skeet@pobox.com> ha scritto nel messaggio
news:MPG.20ba7bed8b3b8597131@msnews.microsoft.com...
[quoted text, click to view]

So we can safely say that there is no "Premature garbage collection" as the
OP's title states.

[quoted text, click to view]
Re: Prematurely garbage collection Jon Skeet [C# MVP]
5/21/2007 6:04:22 PM
[quoted text, click to view]

It's premature compared with the OP's *expectation* which is pretty
important, IMO.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
Re: Prematurely garbage collection Barry Kelly
5/22/2007 12:00:00 AM
[quoted text, click to view]

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

--
AddThis Social Bookmark Button