all groups > c# > april 2008 >
You're in the

c#

group:

Singleton and GC


Singleton and GC David
4/9/2008 9:18:01 PM
c#:
Consider the following singleton:

public sealed class Foo {
private static readonly Foo instance = new Foo();
private Foo() {
//Do init work
}

public static Foo Instance {
get {
return instance;
}
}
}

Once the Instance property is accessed like so:

Foo fooInstance = 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) because the
private static field - instance - will always hold a reference to the
instance of Foo.

Is this correct?

Re: Singleton and GC Peter Duniho
4/9/2008 9:41:23 PM
On Wed, 09 Apr 2008 21:18:01 -0700, David <maverick88@noemail.noemail> =

[quoted text, click to view]
e =

[quoted text, click to view]

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?

Re: Singleton and GC Michael Justin
4/10/2008 7:32:32 AM

[quoted text, click to view]

I think yes. A live reference to an object will make it ineligible for
GC. This means you can have a sort of memory leaks in C# - if you keep
references to objects you don't need.

Hope this helps(tm)
--
Michael Justin
SCJP, SCJA
betasoft - Software for Delphiâ„¢ and for the Javaâ„¢ platform
Re: Singleton and GC Marc Gravell
4/10/2008 7:52:17 AM
[quoted text, click to view]

It will indeed not be garbage collected; re lasting
for the lifetime - it can't outlive it, but note that
it won't be created until the static class is first
accessed(there are some more complex rules, but
that is the rough version...)

Re: Singleton and GC Arne Vajhøj
4/10/2008 8:29:16 PM
[quoted text, click to view]

Not quite.

..NET does not use reference counting. .NET checks whether an object is
reachable.

The instance of Foo will always be reachable via Foo.Instance and will
therefore never be garbage collected.

(if the app domain where the class is loaded is unloaded then it becomes
unreachable and the instance can be garbage collected, but that is a
special case)

AddThis Social Bookmark Button