We are reading huge files in to memorystream and reusing them across mulitple locations in the code. Just wondering if it is a better idea to get byte[] of files and store them in a hashtable for such a scenario. This way we could close the memorystream when we are done and just recreate one from hashtable when needed. Just wondering if there are any drawbacks with this method. Thanks N
A:
Just wondering if there are any drawbacks with this method.
For huge files? How large is “huge”? How much main memory can you use?
Konrad Rudolph
2010-02-11 11:59:40
100 MB, 200 MB for now we r dealing with.
np
2010-02-11 12:16:13
+1
A:
One advantage of using a byte[]
rather than a MemoryStream
is that the MemoryStream
has more state - it has a cursor. In particular, two threads could easily read from the same byte array (copying the section they're interested in etc) whereas if they tried to use Stream.Read
concurrently, they may not get the expected results.
The downside of both of these is that they're mutable :(
Jon Skeet
2010-02-11 12:00:29
Is there any advantage converting to a base64string from byte[] and caching it rather than byte[].
np
2010-02-11 12:50:29
@np: Immutability I guess, but that's all I can think of. It would be better to cache the byte array and give copies to callers if you want to avoid stompage.
Jon Skeet
2010-02-11 13:44:11
A:
If the files are really that big, then you're better off not keeping them in memory in the first place. Instead you should be reading them as and when they're needed, in order to reduce memory usage.
Will Vousden
2010-02-11 12:01:16