On Wed, 09 Apr 2008 21:18:01 -0700, David <maverick88@noemail.noemail> =
[quoted text, click to view] wrote:
> [...]
> Foo fooInstance =3D Foo.Instance;
>
> and assuming fooInstance is not used anywhere else in the application,=
>
> the instance of Foo is *never* garbage collected and will last for the=
> lifetime of the AppDomain (which could be an ASP.net AppDomain) becaus=
e =
[quoted text, click to view] > the
> private static field - instance - will always hold a reference to the
> instance of Foo.
>
> Is this correct?
I admit, I don't know the authoritative answer to the question. However=
, =
I don't think I would make the assumption that the instance won't be =
collected.
For sure, the JIT compiler is intelligent enough to recognize when local=
=
variables are not longer used, allowing an instance referenced only by a=
=
local variable's that not ever used again to be collected, even though =
syntactically the variable is still in scope.
Even if the compiler doesn't do this now, I'm not aware of any reason wh=
y =
if code analysis shows a static field is never actually used again, the =
=
garbage collector wouldn't recognize the instance as no longer reachable=
=
and thus eligible for collection. It's entirely possible that feature =
exists today.
I guess for me the big question is, why do you want to know? Even if yo=
u =
could know the answer, and the answer today is "the instance won't be =
collected", I don't know of any reason you should be able to rely on tha=
t =
behavior.
Is there something that came up where this is relevant?