tags:

views:

335

answers:

4

This is a duplicate of: http://stackoverflow.com/questions/2155688/what-is-a-singleton-in-c

I don't think it is duplicate, as here what I am looking for is best strategy for releasing/disposing the Singleto object when not in use.

How to implement a Singleton so that object instance can be released/disposed when all it's references are not in use? And when ever any body wants a Singleton instance it is lazy loaded on demand.

+3  A: 

Singletons are not supposed to be disposed of dynamically: once created, they exist till the end of the application's lifetime. Singleton means there is one and only one instance of it.

Even if your Singleton reserves a resource which you want to dynamically release and re-reserve, you shouldn't destroy and rec-create the Singleton instance. That would contradict with the common meaning and usage of the pattern, which can (at best) cause communication problems in your team, or (at worst) subtle bugs in your app.

Instead, you could have the Singleton object internally manage that resource: release it if it hasn't been used for some time, or if its reference count drops to 0.

You should also consider using a Factory instead to access that resource. This gives you much more freedom to control the handling of the resource in question. You can also reuse the created object internally, in effect keeping object count to at most 1.

Péter Török
Just a thought, that if Singleton is using some resource on server end, which should be released when not required. So in that case if Singleton instance is not in use, release the object. And whenever required again create it's instance.
Niren
@Niren I updated my post.
Péter Török
yes that is what I was thinking about.
Niren
+1  A: 

maybe this will will help you:

http://en.csharp-online.net/Singleton_design_pattern:_Eager/Lazy_Singleton

and this one is about lazy and thread safe singleton:

http://www.yoda.arachsys.com/csharp/singleton.html

grapkulec
Lazy load is one part, but what if Singleton object is not in use, how to release it to free up the resource in use?
Niren
I always write Release method to my singletons where I release any resources allocated by singleton. But I call it during application shutdown code because I didn't yet ecounter situation in which releasing singleton earlier would be necessary.
grapkulec
+3  A: 

From Implementing the Singleton Pattern in C#:

public sealed class Singleton
{
    Singleton()
    {
    }

    public static Singleton Instance
    {
        get
        {
            return Nested.instance;
        }
    }

    class Nested
    {
        static Nested()
        {
        }
        internal static readonly Singleton instance = new Singleton();
    }
}

As far as the release part it is contradictory to the singleton pattern because if you release the instance the next time someone tries to access it it will get a new instance which violates the singleton pattern.

Darin Dimitrov
So do you mean according to singleton pattern the object should be created ones and should be used for the period of application life cycle. It should not or cannot be destroyed and another instance should not be created?
Niren
Yes, that's what singleton means.
Darin Dimitrov
@Darin - I don't agree. singleton means that there is only one instance of that class at the time, so if you release it, next use of it will create another instance. it doesn't mean that singleton instance have to be alive whole time application is running.
grapkulec
of course it depends what is a role of your singleton in application, if it keeps application settings it would be silly to release it during runtime, but if it serves another purpose why not?
grapkulec
+3  A: 

As some other answers say, Implementing the Singleton Pattern in C# is one of the best resources for Singletons in general.

If you want your singleton to be released when it's not referenced anywhere else, you might take your favorite pattern off the aforementioned site and wrap the instance into a WeakReference, for example something like:

public sealed class Singleton
{
    static private readonly WeakReference _instanceReference =
        new WeakReference(Singleton.LoadInstance());
    static public Singleton Instance
    {
        get { return Singleton.GetInstance(); }
    }

    static private Singleton() { }

    static private Singleton LoadInstance()
    {
        // load from expensive resource;
        return new Singleton();
    }

    static private Singleton GetInstance()
    {
        Singleton result = _instanceReference.Target as Singleton;
        if (result == null)
        {
            // TODO: consider thread safety
            result = LoadInstance();
            _instanceReference.Target = result;
        }

        return result;
    }

    private Singleton()
    {
        //
    }

}

Be aware that consumers most likely would merely call your Singleton.Instance and won't create a reference by themselves, which means your resource would get reloaded quite often. I guess this pattern works best if the Singleton Instance would sometimes be a member of certain classes you pass around.

herzmeister der welten
Instead what if a static reference count is maintained in Singleton class. And a proxy is used between the consumer and singleton. Where Proxy is disposed when not required. And in case if singleton instance can be destroyed when reference count is == 0. Can this work?
Niren
Yes, a reference counter _might_ work here as well, but basically, the `WeakReference` does all that hassle for you, so why bother in that case?
herzmeister der welten