On Wed, 17 Sep 2008 13:40:55 -0700, paradigmshift <ch*********@gmail.com>
wrote:
I am looking at how to move data from a file to a memory stream. This
data will be needed many times over and over so i don't want to have
to rely on the system to keep the information cashed for access. Kind
of an easy question for pros but thats why I figured I would ask here.
Just read it from the FileStream and write it to the MemoryStream. As an
optimization, start by setting the Capacity property of the MemoryStream
to the length of the original FileStream. That will allow you to avoid
intermediate reallocations as the MemoryStream is written to.
That said, it's not really clear what advantage you intend to have by
copying all of the data to a MemoryStream. The system will still wind up
caching the data, but through a different mechanism (virtual memory rather
than via the file system). Copying all of the data from a file to a
memory block simply increases the memory pressure for the overall system,
which may in fact result in counter-productive behavior (e.g. data being
flushed from memory and needing to be reloaded later).
I encourage you to do some real-world performance comparisons if you
really want to try this. I suspect that you will find that the
performance improvement is minimal, assuming you don't actually make
things worse.
Pete