> I am having really tough time finding anywhere on the web
> concrete explanation (if any) of how Garbage Colletor
> decided for C++ managed objects when object is ready to be
> released, and why? Refer to simple example below with
> questions inside comments.
It's an optimization to make the object available for collection after it's
last used, but still in scope.
Change your example a little so you can see when the instance of MyGCClass
is collected, and you'll see that in a debug build, the object cannot be
collected until it's out of scope, but in a release build it can be
collected as soon as it's no longer needed.
In Debug build:
D:\projects\test_mcc\Debug>test_mcc
Something Done
GC Finished
simple_unmanaged_function finished
Object Collected
In Release build:
D:\projects\test_mcc\Release>test_mcc
Something Done
Object Collected
GC Finished
simple_unmanaged_function finished
David
#include "stdafx.h"
#using <mscorlib.dll>
using namespace System;
__gc public class MyGCClass
{
~MyGCClass()
{
Console::WriteLine(S"Object Collected");
}
public:
void DoSomething()
{
Console::WriteLine(S"Something Done");
}
};
void simple_unmanaged_function()
{
int x = 0;
MyGCClass* obj = new MyGCClass();
obj->DoSomething();
GC::Collect();
GC::WaitForPendingFinalizers();
Console::WriteLine(S"GC Finished");
x = 5;
}
int _tmain()
{
simple_unmanaged_function();
Console::WriteLine(S"simple_unmanaged_function finished");
GC::Collect();
GC::WaitForPendingFinalizers();
return 0;
}
Boris - 31 Oct 2003 17:47 GMT
my_gc_obj.DoSomething();
if DoSomething() doesn't access/modify any of the
my_gc_obj member variables, can GC release my_gc_obj
before DoSomething() returns?
Or even, if optimization is so smart, can GC release
my_gc_obj before DoSomething() is called?
-Boris
>-----Original Message-----
>
[quoted text clipped - 74 lines]
>
>.
Stu Smith - 05 Nov 2003 17:29 GMT
Definite yes to the first question (and if it's a problem you may want to
look at GC.KeepAlive).
The second question... if it's a virtual call then presumably no, because
the object's vtable is needed. If it's not.... I guess so, but it's just
that: a guess.
> my_gc_obj.DoSomething();
>
[quoted text clipped - 94 lines]
> >
> >.
JuQiang - 25 Nov 2003 01:08 GMT
the first question,GC will NOT work as soon as your variable out of
scope.The time is according to your Generations in GC
3 generations worked in GC.
Generation 0/1/2, if gen0 is filled, GC recycled and move it to gen1.if gen1
is filled,gc recycled and move it to gen2.
> my_gc_obj.DoSomething();
>
[quoted text clipped - 94 lines]
> >
> >.
Joe,
| __gc public class MyGCClass
| {
[quoted text clipped - 15 lines]
| // at this point, if it is not being used
| // until the end of the function?
At this point you still have a reference to the MyGCClass instance, so it
will not be garbage collected. If you set obj = null, then the reference is
removed and the object is ready to be gc'ed.
| // Must GC postpone collecting the instance
| // until obj reference to goes out of
| // of scope? i.e. for function to return
The object remains reachable (ie not gc-able) while there is still a
reference to it. The reference is removed if you explicitly set it to null
or if it goes out of scope.
| // What if different optimizations are
| // set in "project"->"Property"->
| // "c/c++"->"Optimizations"?
| //
These will have no effect on garbage collection.
| x = 5;
|
| // Is this (set to NULL) necessary to
| // mark obj as not referencing and
| // available for GC?
| // Or going out of scope is sufficient?
Going out of scope is sufficient.
For a really good explaination of how the garbage collector works, you may
way to take a look at a book called "Applied Microsoft .Net Framework
Programming" by Jeffrey Richter. ISBN 0-7356-1422-9.
Please let me know if you have any further questions.
Thanks,
Michael Green
Microsoft Developer Support
This posting is provided "AS IS" with no warranties, and confers no rights.
Please reply to newsgroups only.
David Browne - 01 Nov 2003 16:20 GMT
> Joe,
>
[quoted text clipped - 29 lines]
> reference to it. The reference is removed if you explicitly set it to null
> or if it goes out of scope.
Except in one case (the case the poster was asking about), where the object
is still has a reference in scope, but the execution has passed the last use
of the refrence. In that case the CLR can collect the object before the
reference goes out of scope. It is an optimization and an exception to the
normal rules of garbage collection.
David
Ori Gershony [MSFT] - 06 Nov 2003 21:03 GMT
An object can be collected as soon as the JIT compiler can verify that it
can no longer be accessed. This is the rule and not the exception.
Sometimes this happens before the object goes out of scope (as in the
example below). Sometimes this can happen after the object goes out of
scope (if the JIT compiler doesn't track the lifetime of all the variables
in the method, it will not know when they die--not tracking the lifetime of
all variables is a JIT-time optimization for large methods).
If you want to be 100% sure that an object can be collected, you should set
it to NULL. Otherwise the JIT compiler will do its best to release the
reference as soon as possible, but within the constraints of its
implementation.
Hope this helped.
-- Ori.
--------------------
>From: "David Browne" <davidbaxterbrowne no potted meat@hotmail.com>
>References: <043001c39f2d$cf28d410$a301280a@phx.gbl>
<mTIRP9AoDHA.2624@cpmsftngxa06.phx.gbl>
>Subject: Re: C++ and Garbage Collection?
>Date: Sat, 1 Nov 2003 09:20:55 -0600
[quoted text clipped - 7 lines]
>NNTP-Posting-Host: 199.34.81.136
>Path:
cpmsftngxa06.phx.gbl!cpmsftngxa09.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP10.
phx.gbl
>Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.clr:8530
>X-Tomcat-NG: microsoft.public.dotnet.framework.clr
[quoted text clipped - 41 lines]
>
>David

Signature
This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm
Note: For the benefit of the community-at-large, all responses to this
message are best directed to the newsgroup/thread from which they
originated.