Tech Off Post

Single Post Permalink

View Thread: Rich Text Box Font
  • User profile image

    W3bbo wrote:
    Dispose is only used by IDisposable for unmanaged resources used by managed classes. Whilst managed code has destructors, these are always called by the GC and not the user. Unlike C++ where you needed to call delete every time you called new, the CLR GC does this at the next GC interval once an object has no references to it.

    That is strange. I use dispose on the fully managed Image object all the time. Using it seems to get old images cleaned up by the GC much faster.

    For example, I have a little tasktray app that is always running and changes my desktop every 15 minutes. Because XP wanted BMP files and most of my images are jpgs, I use an Image object to load the jpg and save it as a bmp in my profiles temp folder to be used as the current desktop. 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.

    So there seems to be times when Dispose is useful even for manage objects. (It could be that having 2GB of RAM gives me enough headroom that the GC does not think it needs to be cleaned up.)

    It would be nice to know if there are any reasons NOT to use Dispose in cases like this because even the Image object documentation says "This member supports the .NET Framework infrastructure and is not intended to be used directly from your code" But does not elaborate.