I'm working on putting together a singleton class, and I'd like to make sure that the standard managed code garbage collection model works efficiently here.
Would there be circumstances under which I could not rely on standard garbage collection with a singleton object? First, the object contains a reference to itself after the first instantiation. When it's no longer in use by any client, it still has that reference, doesn't it? So does that mean it doesn't get disposed, unless I specifically do something like keep track of external references and manually dispose when there are none?
Second, if the singleton object is using resources, does it have to specifically release references to those resources in order for garbage collection to work properly on those resources? I'm thinking that the answer is no, that as GC goes about its business it looks for references to objects that are no longer there and treats them as invalid. Is that the case? Further with regard to using resources, is there some sort of default timeout period in GC where if an object is inactive for a while it will be destroyed along with any object references it has open?
Any other advice about singletons and memory management in C#?
TIA
Bob
Would there be circumstances under which I could not rely on standard garbage collection with a singleton object? First, the object contains a reference to itself after the first instantiation. When it's no longer in use by any client, it still has that reference, doesn't it? So does that mean it doesn't get disposed, unless I specifically do something like keep track of external references and manually dispose when there are none?
Second, if the singleton object is using resources, does it have to specifically release references to those resources in order for garbage collection to work properly on those resources? I'm thinking that the answer is no, that as GC goes about its business it looks for references to objects that are no longer there and treats them as invalid. Is that the case? Further with regard to using resources, is there some sort of default timeout period in GC where if an object is inactive for a while it will be destroyed along with any object references it has open?
Any other advice about singletons and memory management in C#?
TIA
Bob