8

While using a MemoryStream, I find myself often copying (hence duplicating) data to a temporary array of bytes.

I think it's a little bit of a waste of ressource, because MemoryStream dosen't let you directly access the underlying byte array.

In this situation, what's the real advantage of a MemoryStream? I have read somewhere that it acts like a memory-mapped file. Data is brought from disk only on access, consuming less memory.

Is that true? I don't think so. Maybe it's the case for a FileStream?

Thank you for your clarification.

1
  • 2
    Which language and library is this using? Commented Jun 12, 2009 at 17:25

2 Answers 2

4

For me, the primary advantage of a memory stream is that it grows dynamically, and is optimized to do so. It is a pain to have to copy it out and duplicate memory, but if you primary use of it is to construct a buffer to be handed off at the end of the process, that flaw is amortized somewhat.

I should add, as opposed to a FileStream, MemoryStreams are much, much faster. They are more limited in size than FileStreams, because you generally have vastly more disk space than RAM. So you have to decide whether you need speed or space.

Sign up to request clarification or add additional context in comments.

5 Comments

Well, in both case (MemoryStream or FileStream), the Read and Write methods take a integer for offset and count. So I think they can go up to the same size.
They theoretically can, but remember that MemoryStream is stored in Memory, so when the RAM is full, it starts to page, and this will cause application slowdowns, and after a while, system slowdowns (due to RAM and page being full).
Yep, one funny thing I noticed is that the Length property of a stream is a long (8 bytes). You can't even reference a file that big with the Read and Write methods. I am pretty sure it has something to do with the native win32 API.
You certainly can read a file with a size exceeding the Int32 space with the Read() method - you just have to call it multiple times and write each filled buffer to a MemoryStream, for example. Read() doesn't guarantee to return the exact number of bytes you requested, so you'd call Read() in a loop, anyways.
@ NorthWind: "the Read and Write methods take a integer for offset and count"- offset and count are applied to buffer read to or write from, not to the stream!
3

You can get the underlying byte buffer using the getBuffer function (but only if you created the MemoryStream from a byte array that you provided, which is useful if you want to be able to manipulate the buffer directly)

The only advantage to using a MemoryStream is if you're using an API that's based on streams, of if you need the byte buffer to be able to grow dynamically..

1 Comment

"only if ... from a byte array that you provided" is not true. Check the docs: Returns The byte array from which this stream was created, or the underlying array if a byte array was not provided to the MemoryStream constructor during construction of the current instance. Actually, if you do provide an array on construction and want GetBuffer to return it, you have to use the constructor overload with the publiclyVisible flag and set that to true, otherwise you'll get an UnauthorizedAccessException.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.