Search code examples
c#.netvb.netfilestreamout-of-memory

Implementation of Stream that wraps another stream


Well i guess i ll have to make a trilogy related to the question of mine found at Out of memory exception while loading images

So here comes the second part.

According to the answers - comments found to the post mentioned above, sooner or later any computer will run out of memory while executing my code. :(

But thanks to Google i found a way of applying WrappingStream Implementation. where according to the writer

The BitmapImage keeps a reference to the source stream (presumably so that you can read the StreamSource property at any time), so it keeps the MemoryStream object alive. Unfortunately, even though MemoryStream.Dispose has been invoked, it doesn't release the byte array that the memory stream wraps. So, in this case, bitmap is referencing stream, which is referencing buffer, which may be taking up a lot of space on the large object heap. Note that there isn't a true memory leak; when there are no more references to bitmap, all these objects will (eventually) be garbage collected. But since bitmap has already made its own private copy of the image (for rendering), it seems rather wasteful to have the now-unnecessary original copy of the bitmap still in memory.

So here are a couple of questions i have. (And Before starting down-voting please keep in mind that i am a newbie!)

Could i use the WrappingStream class to avoid out of memory errors? If the first one is true, how can i adopt it and make it work with my example?

Thank you


Solution

  • Could i use the WrappingStream class to avoid out of memory errors?

    No, it is aimed at a different problem.