GC.Collect() and Finalize

前端 未结 5 1596
眼角桃花
眼角桃花 2020-12-01 07:19

Ok, it\'s known that GC implicitly calls Finalize methods on objects when it identifies that object as garbage. But what happens if I do a GC.Collect()

5条回答
  •  青春惊慌失措
    2020-12-01 07:54

    Couple of more points are worth to state here.

    Finalizer is the last point where .net objects can release unmanaged resources. Finalizers are to be executed only if you don’t dispose your instances correctly. Ideally, finalizers should never be executed in many cases. Because proper dispose implementation should suppress the finalization.

    Here is an example for correct IDispoable Implementation.

    If you call the Dispose method of any disposable objects, it should clear all references and Supress the finalization. If there is any not so good developer who forget to call the Dispose method, Finalizer is the life saver.

提交回复
热议问题