Tech Off Thread

22 posts

Rich Text Box Font

Back to Forum: Tech Off
  • User profile image
    Crippled​Smurf

    The Dispose method is used to release resources used by objects, while it's true that you can wait for GC to invoke the finalizer it's generally better to release resources you have obtained as soon as you finish with them, especially when the resource you have is particularly expensive or scarse, this is because you have no control over when GC invokes the finalizer, it could be five minutes, or fove hours.

  • User profile image
    stevo_

    Personally I was going to say finalize was the 'other side' of the constructor. Rather than Dispose, which is a method of granular resource cleanup, such as freeing up unmanaged objects.

    I could be wrong, but I've generally looked at it that way.

  • User profile image
    Yggdrasil

    Jorgie wrote:
     Even though the Image object goes out of scope right after I finish with it, if I don't call the images dispose function my memory use grows to 128MB or higher after running for 24hours or more. Once I started using the image objects dispose function, the application never uses more than 4BM.


    There's a difference between managed objects and managed memory. Even though Image is a managed class, it holds unmanaged resources (in this case, a handle to a GDI object where the bitmap is actually mapped. You can see this by opening Reflector and looking at the Dispose(Boolean) and FromFile(string) methods on the Image object. Lots of unmanaged memory mapping going on.

    Same thing goes for Stream objects - the object itself is managed, but it holds unmanaged resources (file handles and locks, in this case).

    In general principle, you can assume that if a framework class implements IDisposable, it probably holds some unmanaged resource and you SHOULD call Dispose() if you know you're done with it. When in doubt - Reflector. Smiley

  • User profile image
    W3bbo

    Yggdrasil wrote:
    In general principle, you can assume that if a framework class implements IDisposable, it probably holds some unmanaged resource and you SHOULD call Dispose() if you know you're done with it. When in doubt - Reflector. Smiley


    Or better yet, just use C#'s using(){} block.

Comments closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums, or Contact Us and let us know.