-3

I have some doubts related to dispose and finalizer in C# which i am mentioning below:-

1.Apart from unmanaged resources, what is the exact need to use dispose method.Why do we use dispose to release the memory of managed code if there is garbage collector to release the memory.

2.Also, why finalizer is not recommended.Microsoft would have some reasons to develop finalizer feature. in the most of sites i have visited,suggested that finalizer is not recommended. what is the reason.

3.Sometimes, we use only object.dispose to release whereas sometimes we use idisposable interface . why?

4.What is the exact condition we must call dispose method?

Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
Ram
  • 21
  • 3
  • There may be possibility for closing this question, still I tried to edit to make easy to understand peoples what you are trying to ask – Jaydip Jadhav Jul 27 '16 at 11:29
  • 3
    Feels broad. Why don't you ask 4 separate questions (first and second are 99% were asked already)? Last 2 questions are unclear (consider to add some pseudo-/real-code to show what you mean). – Sinatr Jul 27 '16 at 11:35
  • Memory allocated by unmanaged code is not release by the GC. Finalisers should be implemented by classes with unmanaged resources but clients should call `Dispose` promptly so the finaliser does not need to run. There is no `Object.Dispose` method. – Lee Jul 27 '16 at 11:40
  • Does reading http://stackoverflow.com/a/538238/1336590 leave you with any questions? – Corak Jul 27 '16 at 12:02

1 Answers1

0

For your #1: as you correctly wrote on your question, the main reason to use Dispose is to free resources from unmanaged resources (like file handles, database connections, etc), but there is one more case where we can call dispose to do some things related with managed resources and that is disconnecting event handlers. There is a really good explanation about this here.

Answering your #2, finalizers are not recommended because they introduce performance issues and because of that, you should avoid using them if you can use better solutions. As stated in this fragment from "Effective C#" by Bill Wagner:

A finalizer is a defensive mechanism that ensures your objects always have a way to release unmanaged resources

And if you keep reading...

Finalizers are the only way to guarantee that unmanaged resources allocated by an object of a given type are eventually released. But finalizers execute at nondeterministic times, so your design and coding practices should minimize the need for creating finalizers, and also minimize the need for executing the finalizers that do exist.

So the finalizer seem to be in the only thing you can do to make sure the unmanaged resources are released, so maybe it was the reason you were looking for (I don't really know the Microsoft reason to do it, sorry).

To answer your #3 I would need an exact code example to what you really mean, but I wil try to guess. I suppose you are talking about the next two different scenarios:

  • Calling myObject.Dispose() after using it, explicit way. For example, we can create an instance, use it and then call Dispose:

    myObject = new MyObject()
    
    // More code here...
    
    myObject.Dispose();
    

    That will be ok if you are sure that between the creation of your instance and the calling to the Dispose method there is no exception in your code, which could cause the call to Dispose to be missed. Of course you can always use a finally block:

    try {
      MyObject myObject = new MyObject()
      (...)
    
    }
    catch (Exception) {
      // manage exception
    }
    finally {
      if (myObject != null)
        myObject.Dispose();
    }
    
  • Calling Dispose using the IDisposable interface through using. It is basically the same that the previous with the finally block, but it will be created "automatically":

    using (MyObject myObject = new MyObject()) {
       // your code here
    }
    

You can check the docs here.

And answering your #4. I think that this is a good answer, but do not forget to read the comments. So, in short, if it has a Dispose method, it should be called.

Community
  • 1
  • 1
BitExodus
  • 749
  • 6
  • 10
  • Hi, BitExodus, thank you very much for your valuable answer. Can not we use Dispose for to release the memory of managed resource explicitly. I mean if i want to release the memory of object of managed resource explictly ,cant i use dispose. – Ram Jul 27 '16 at 13:19
  • The memory used by managed classes is claimed by the Garbage Collector when the instance of the object is not needed anymore. Calling the "Dispose" method will have no effect in that case because it is used for unmanaged resources. If you need to release the memory of a managed instance the best strategy is to let it fall out of scope or assign a "null" value to so the Garbage Collector will claim the memory used by that instance. – BitExodus Jul 27 '16 at 14:17
  • Hi BitExodus, some of related posts suggested that we can use dispose with Managed code as well to release the memory explicitly. – Ram Aug 03 '16 at 05:34
  • As far as I know it is not possible, but if you show me those posts I will take a look :). Maybe there are some new features or something I am missing. – BitExodus Aug 03 '16 at 08:32