This is a duplicate of: 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.
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:
Be aware that consumers most likely would merely call your
Singleton.Instanceand 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.