5

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? :)

Peter
  • 912
  • 3
  • 11
  • 29
  • 1
    I believe its when the application terminates (along with the rest of the statics) (assuming the reference isn't removed) – Sayse Aug 19 '13 at 12:40
  • It will never be garbage collected unless and until you set `s_Instance` to `null` and you don't have any other references of the same – Sriram Sakthivel Aug 19 '13 at 12:43
  • 3
    As far as your code is concerned: Never. And this should not matter to you. When you worry about Dispose (!= collect) then ask about that. – H H Aug 19 '13 at 12:46
  • this had been answered really perfectly at http://stackoverflow.com/a/538238/1856345 it contains everything you will need to know about GC and Idisposable interface, how to use both at managed and unmanaged resources edit: but answering your question , no GC doesn't deallocate static fields – Andrew Aug 19 '13 at 12:48
  • 1
    This question isn't about `IDisposable`, it's about when static fields are eligible for collection. – Lee Aug 19 '13 at 13:20
  • i know but it's all was explained there, you may simply ignore the IDisposable part and just focus on what you want there, but answering your question , no GC doesn't deallocate static fields – Andrew Aug 19 '13 at 13:24
  • C# is based on Java, but I don't think a C# garbage collector question should be marked as a duplicate of a Java garbage collector question. – Denise Skidmore Feb 13 '18 at 14:33

2 Answers2

6

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.

Community
  • 1
  • 1
Timotei
  • 1,909
  • 2
  • 22
  • 31
1

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

Community
  • 1
  • 1
andy
  • 5,979
  • 2
  • 27
  • 49