Each object is going to be added in Finalize queue before it is Garbage Collected? RRS feed

  • Question

  • I have following question :

    Each object is going to be added in Finalize queue and F-reachable queue before collection even if user has not overridden Finalize method (and because of Object.Finalize is present)?

    If this is the case then is it not unnecessary burden? Can Such objects not be garbage collected in first Collect cycle directly without calling their Finalize method?

    I read an article about garbage collection from following link :

    From this article I thought that if you do not override Finalize method in your class then object of your class will not be finalizable. And it will be Garbage collected in one Collect cycle. But then question comes whether microsoft considers only overridden Finalize methods (and not the Object.Finalize() one!!) during Garbage Collection?

    Thanks and Regards,
    Vishal Shah
    Monday, July 27, 2009 7:04 AM


All replies

  • Hello,

    Only those objects that implement Finalizer are added to F-reachable queue.
    Vitaliy Liptchinsky
    Monday, July 27, 2009 9:05 AM
  • Objects are only considered finalizable when the Finalize method is overridden. So for the majority of types in the framework it means they are not finalizable and indeed will be collected in a single collect, where as finalizable objects always survive a single collect and will be promoted to the next generation (as the article probably explains).
    Visit my blog:
    Monday, July 27, 2009 9:06 AM
  • Hi,

    Suppose you have to clean a resource before destroying your objects so you will write cleaning code inside a destructor which will be translated to finalize by the CLR

    GC checks whether any of the unreachable objects has a destructor that needs to be run (a process that is called finalization).
    So any unreachable object that requires finalization is placed in a special queue that is called the freachable queue. Otherwise it will not be inserted in the queue.

     GC starts calling finalize of the objects existing in the queue before destroying them.

    The problem in this case you can’t determine when the GC will run and call finalize, in some case if your resources are critical you need to make sure that your class’s client can free this resource so you will implement IDisposable interface which enable your client to call Dispose() but the problem is what if they forget to call Dispose()? the resource will not clean, so the following code is implement IDisposable interface to enable class clients to call Dispose() and write destructor to clean your resource in case of your client forget to call Dispose() and make flag so if the client call Dispose() it will Suppress the finalize by GC


    private bool _disposed = false;
                public void Dispose()
                protected virtual void Dispose(bool disposing)
                    if (!_disposed)
                        if (disposing)
                            // Dispose managed resources here.
                        // Dispose unmanaged resources here.
                        // Set the _disposed flag to prevent subsequent disposals.
                        _disposed = true;
                // Finalization code.


    We are volunteers, if the reply help you mark it as your answer. thanks!!
    My Blog
    Monday, July 27, 2009 9:41 AM
  • Hello Vitaliy and Steven ,

    So can we think that reference of the object will be added to Finalize queue only when class of that newly created object has overridden Finalize method. In short CLR will identify whether this class has overriden the Finalize or not and if class has overridden then and only then CLR will add refernce of the newly created object to Finalize queue. If class has not overridden Finalize then class will have imlpementation of System.Object class's Finalize method and in that case object of that class will not be considered as Finalizable and CLR will not add reference for such objects in Finalize queue.

    Please correct me if I am wrong.

    Thanks and Regards,
    Monday, July 27, 2009 10:24 AM
  • This is correct.

    Visit my blog:
    Monday, July 27, 2009 10:35 AM
  • Thank you for Reply.


    Monday, July 27, 2009 10:55 AM