0

I'm pretty new to c#, so i apologise if the answer to my question is painfully obvious...

I'm wondering if there is a difference between the way garbage collection treats the following two Image object references:

Size imgSize = System.Drawing.Image.FromStream(FileUpload01.FileContent).PhysicalDimension.ToSize();

, and

Size imgSize;
using (System.Drawing.Image img = System.Drawing.Image.FromStream(FileUpload01.FileContent))
{
    imgSize = img.PhysicalDimension.ToSize();
}

so in the first example, there is no stored image reference, and in the second, the using clause should force disposal.

Will there be any difference as to when GC will be able to clean up these two images?

Thanks in advance...

[EDIT:] To be clear, since this was flagged as duplicate, my question is how GC treats the image object in the first example, since after that statement has executed, i can no longer access the image object. (i think this is different from a general discussion on GC's treatment of IDisposable implementations).

CJ K
  • 146
  • 11
  • 1
    From references point of view shouldn't be a difference, but if `img` actually requires to be disposed, then in first approach object will not be disposed. – Fabio Jan 14 '19 at 00:52
  • Possible duplicate of [what is relation between GC, Finalize() and Dispose?](https://stackoverflow.com/questions/2344240/what-is-relation-between-gc-finalize-and-dispose) – mjwills Jan 14 '19 at 01:00
  • 1
    Your first example is terrible, and you have a big un-manged GDI resource open that is never closed or disposed by you, if you called this method a lot, you will get a GDI out of memory error, not to mention other problems – TheGeneral Jan 14 '19 at 01:02
  • 2
    At this stage in your learning, don't worry about how the GC works as it works well. Worry about using your the `using` statement where ever you can, disposing everything at the first possible time your can, and not putting everything on the one line – TheGeneral Jan 14 '19 at 01:07
  • `there is no stored image reference,` The GC doesn't care whether there is a stored image reference or not - it cares whether or not the object is reachable (https://blogs.msdn.microsoft.com/cbrumme/2003/04/19/lifetime-gc-keepalive-handle-recycling/). In practical terms, whether or not there is an explicit `img` variable won't make a difference in terms of reachability for a Release build (it will in Debug build, but that is a side issue). – mjwills Jan 14 '19 at 01:23
  • thanks @mjwills, i think that answers my question. So the Image in example 1 is not disposed even though it is no longer accessible in my code. – CJ K Jan 14 '19 at 02:21
  • 2
    Correct. `IDisposable.Dispose` will usually be called in one of two ways. Using `using` or calling it directly. Now, a finaliser may, much later, try and do some cleanup (which is roughly equivalent to the cleanup done by `IDisposable.Dispose`) - but it is not deterministic. So you should (almost) always use `using`. – mjwills Jan 14 '19 at 02:23
  • 1
    `my question is how GC treats the image object in the first example` If using a Release build, the GC treats them basically the same way, since they both become unreachable at roughly the same point. – mjwills Jan 14 '19 at 02:55
  • Possible duplicate of [Right way to dispose Image/Bitmap and PictureBox](https://stackoverflow.com/questions/2808753/right-way-to-dispose-image-bitmap-and-picturebox). ALSO: To repeat what everyone else has said: There's a *BIG* difference between the 1st example and the second (where "using" promptly closes the resource). As a general rule of thumb, you should *ALWAYS* explicitly close (or use "using") with *ANY* resource (like a file, a database connection... or a disposable GC object). – paulsm4 Jan 14 '19 at 03:54

0 Answers0