11
 public static class stClass
{
    static Class1 obj = new Class1();

    public static int returnSomething()
    {
        return 0;
    }
}

When will the Class1 instance obj in stClass get garbage collected, if i am calling the static function stClass.returnSomething() in some other non static class ?

Note: Class1 is not static

3 Answers 3

18

Never, as obj does not implement IDisposable.

If you mean when will obj get garbage collected then the answer is still never - static fields are never garbage collected and so the object that obj references will only become eligible for garbage collection if you set obj to be null (or some other object) and have no other references to that object:

obj = null;

(or if your app domain is unloaded / the process ended)

2
  • Good point about IDisposable, but I suspect the poster means when the object will be garbage collected. Mar 10, 2011 at 7:56
  • 3
    "will only be garbage collected if you set obj to be null" - this is slightly misleading. The object will only become eligible for garbage collection when you set obj to be null. The actual time of collection is indeterminate.
    – MattDavey
    Mar 10, 2011 at 9:31
3

It will never get disposed as it doesn't implement IDisposable. However it will get garbage collected. This will happen when you exit your application or you destroy the AppDomain that the class was created in.

3
  • 2
    Actually i dont think it will get garbate collected as in per garbage collector. The app will just stop, the process be discarded.
    – TomTom
    Mar 10, 2011 at 7:59
  • seems I misunderstood the question a bit. I thought you were asking when stClass will get disposed. The object "obj" will get disposed when you explicitly call dispose on it OR when you remove all the references to it (i.e assign obj = null), it will get disposed before it gets garbage collected.
    – Can Gencer
    Mar 10, 2011 at 8:00
  • TomTom, this is an interesting question. I think the behavior here is different depending on if app ends gracefully, should be quite easy to test. As obviously if you kill it the memory won't be garbage collected.
    – Can Gencer
    Mar 10, 2011 at 8:02
0

If Class1 implements the IDisposable interface & has a finalizer, the runtime will attempt to call the finalizer when the appdomain is unloaded*. If Class1 follows the dispose pattern properly, the finalizer should call dispose(false). This would be the correct time to unload any unmanaged resources being used by the Class1 instance.

However, when dealing with an object that uses unmanaged resources, you should strive to take more control over the object lifecycle rather than just letting the finalizer run when the appdomain shuts down.

*Even the finalizer is not guaranteed to be called when the app shuts down..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.