Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When will the garbage collector collect a Singleton? [duplicate]

I have some SomeSingleton class in C# (.NET 3.5 if it matters) and code:

foo()
{
    ...
    SomeSingleton.Instance.DoSomething();
    ...
}

My question is: when will Garbage Collector collect this Singleton object?

p.s: code of SomeSingleton:

    private static SomeSingleton s_Instance = null;
    public static SomeSingleton Instance
    {
        get 
        {
            if (s_Instance == null)
            {
                lock (s_InstanceLock)
                {
                    if (s_Instance == null)
                    {
                        s_Instance = new SomeSingleton();
                    }
                }
            }
            return s_Instance;
        }
    }

Thanks for help!

EDIT (with explanation):

In Widnows Service I have code:

   ...
   FirstSingleton.Instance.DoSomething();
   ...

public class FirstSingleton
{
    (Instance part the same as in SomeSingleton)
    public void DoSomething()
    {
        SomeSingleton.Instance.DoSomething();
    }
}

What I want to achieve: I do not care what happens with FirstSingleton, but SomeSingleton starts Timer with first use of it, so I need SomeSingleton to exist (so the timer can run new thread every period of time) as long as my service is running.

As I understand from your answers all of that will happen because reference to my FirstSingleton and SomeSingleton is static, and singletons will not be collected by GC until service stops, am I right? :)

like image 814
Peter Avatar asked Aug 19 '13 12:08

Peter


People also ask

When should a singleton be garbage collected?

No, a Singleton will not be garbage collected. Back in the old Java 1.1 days they could be, but now a Singleton will only be garbage collected when there are no references to its ClassLoader.

Can singleton class be garbage collected?

Singleton class has a static reference to the instantiated singleton object and hence it will never be garbage collected unless ofcourse as Jon Skeet as stated that the context that loaded this class (class loader) is itself eligible for garbage collection in which case that static reference will not longer be a GC ...

What is the purpose of Singleton?

The Singleton's purpose is to control object creation, limiting the number to one but allowing the flexibility to create more objects if the situation changes. Since there is only one Singleton instance, any instance fields of a Singleton will occur only once per class, just like static fields.

How does garbage collector resolves circular reference issue?

To handle the problem of circular references in C#, you should use garbage collection. It detects and collects circular references. The garbage collector begins with local and static and it marks each object that can be reached through their children. Through this, you can handle the issues with circular references.


2 Answers

This question can be answered by this one: Do static members ever get garbage collected?

Basically, the instance will be destroyed when the containing AppDomain will get destroyed.

like image 142
Timotei Avatar answered Oct 06 '22 04:10

Timotei


Objects referenced by static variables will only be garbage collected when the relevant AppDomain is garbage collected. In client applications, there's often just a single AppDomain which lives for the duration of the process. (An exception is when the application uses a plug-in architecture - different plug-ins may be loaded in different AppDomains and the AppDomain may be unloaded later.)

Refer

like image 30
andy Avatar answered Oct 06 '22 05:10

andy