none
Dispose usage on managed resources RRS feed

  • Question

  • Hi,

    In my application i donot have/use unmamaged resources. is it necessary to implement dispose method ? Will not the GC clear it automatically ? if i have to use dispose what's benefit ?


    Vijaykishan | http://v2attechhorizon.blogspot.com
    Friday, February 19, 2010 5:59 AM

Answers

  • For the majority of the objects that your application creates, you can rely on the garbage collector to automatically perform the necessary memory management tasks. However, unmanaged resources require explicit cleanup. The most common type of unmanaged resource is an object that wraps an operating system resource, such as a file handle, window handle, or network connection. Although the garbage collector is able to track the lifetime of a managed object that encapsulates an unmanaged resource, it does not have specific knowledge about how to clean up the resource. When you create an object that encapsulates an unmanaged resource, it is recommended that you provide the necessary code to clean up the unmanaged resource in a public Dispose method. By providing a Dispose method, you enable users of your object to explicitly free its memory when they are finished with the object. When you use an object that encapsulates an unmanaged resource, you should be aware of Dispose and call it as necessary. For more information about cleaning up unmanaged resources and an example of a design pattern for implementing Dispose, see Garbage Collection.

     http://msdn.microsoft.com/en-us/library/f144e03t.aspx
    Manish Sati
    Friday, February 19, 2010 6:19 AM

All replies

  • For the majority of the objects that your application creates, you can rely on the garbage collector to automatically perform the necessary memory management tasks. However, unmanaged resources require explicit cleanup. The most common type of unmanaged resource is an object that wraps an operating system resource, such as a file handle, window handle, or network connection. Although the garbage collector is able to track the lifetime of a managed object that encapsulates an unmanaged resource, it does not have specific knowledge about how to clean up the resource. When you create an object that encapsulates an unmanaged resource, it is recommended that you provide the necessary code to clean up the unmanaged resource in a public Dispose method. By providing a Dispose method, you enable users of your object to explicitly free its memory when they are finished with the object. When you use an object that encapsulates an unmanaged resource, you should be aware of Dispose and call it as necessary. For more information about cleaning up unmanaged resources and an example of a design pattern for implementing Dispose, see Garbage Collection.

     http://msdn.microsoft.com/en-us/library/f144e03t.aspx
    Manish Sati
    Friday, February 19, 2010 6:19 AM
  • Thanks manish.

    My question is about the necessity of dispose method for managed resources.


    Vijaykishan | http://v2attechhorizon.blogspot.com
    Friday, February 19, 2010 6:48 AM
  • Managed objects are managed by GC. You should not write their Dispose method unless they encapsulate/contain unmanaged resoruces.

    -Karel

    Friday, February 19, 2010 7:08 AM
    Moderator
  • Unfortunately, that's not entirely accurate.  Types with either unmanaged or disposable state should be disposable.  If a class has a field whose value implements or may implement IDisposable, and the class is responsible for managing the lifetime of that value, then the class should be disposable.

    Friday, February 19, 2010 12:37 PM
  • Yes, that's what I meant by encapsulating unmanaged resources (indirectly).
    Friday, February 19, 2010 4:31 PM
    Moderator
  • How to Implement IDisposable and Finalizers: 3 Easy Rules:
      http://nitoprograms.blogspot.com/2009/08/how-to-implement-idisposable-and.html

    Rule 1 describes when it's not necessary; Rule 2 describes how to do it for managed resources.

           -Steve
    Programming blog: http://nitoprograms.blogspot.com/
      Including my TCP/IP .NET Sockets FAQ
      and How to Implement IDisposable and Finalizers: 3 Easy Rules
    Microsoft Certified Professional Developer

    How to get to Heaven according to the Bible
    Friday, February 19, 2010 5:03 PM