If you have a MemoryStream, is it beneficial to Close() it after use ?
Will this reclaim some used memory ?
I've also seen some newsgroup posts which suggest that you can reclaim
memory by calling SetLength(0) then Capacity = 0.
If you use up "a lot of memory" with MemoryStreams, are any of
the above useful, or should you just let the garbage collector
deal with it ?
Thanks,
Stephen
Jon Skeet - 26 Sep 2003 14:06 GMT
> If you have a MemoryStream, is it beneficial to Close() it after use ?
> Will this reclaim some used memory ?
I don't believe so, given the docs which say:
<quote>
The buffer is still available on a MemoryStream once the stream has
been closed.
</quote>
> I've also seen some newsgroup posts which suggest that you can reclaim
> memory by calling SetLength(0) then Capacity = 0.
That may well make the buffer available for collection, although I
wouldn't usually use it - I'd just let the whole stream be collected
and create a new MemoryStream if necessary.
> If you use up "a lot of memory" with MemoryStreams, are any of
> the above useful, or should you just let the garbage collector
> deal with it ?
I would personally still Close the stream, just so that it's consistent
with the use of other streams - it's not really necessary though, as
far as I can see. I would then just let the stream become eligible for
garbage collection.

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Steve - 26 Sep 2003 19:54 GMT
Work with the assumption that anything implementing
IDisposable should be disposed when you're done
with it.
In the case of MemoryStream MSDN states that: "The
buffer is still available on a MemoryStream once
the stream has been closed".
If you frequently allocate and delete large
memorystreams, consider keeping them in a pool
and re-use them. That will keep the GC happy.
> If you have a MemoryStream, is it beneficial to Close() it after use ?
> Will this reclaim some used memory ?
[quoted text clipped - 7 lines]
> Thanks,
> Stephen
Jon Skeet - 27 Sep 2003 07:21 GMT
> If you frequently allocate and delete large
> memorystreams, consider keeping them in a pool
> and re-use them. That will keep the GC happy.
Well, in some ways it will - in some ways it'll make the GC cross, as
it were. The GC is designed around the assumption that lots of small
objects are built with short lifetimes, and that a few objects are
built with long lifetimes.
By pooling objects, you're artificially increasing their lifetimes, and
so you end up with more objects in generation 2, which is slower to
access due to write barriers, and is collected less frequently (so that
when your objects finally *are* available for collection, the GC won't
notice for ages).

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Rico Mariani [MSFT] - 26 Sep 2003 20:39 GMT
The memory stream has an internal byte array that it uses for its work. If
you are finished with the memory stream the easiest thing to do is simply
null your reference(s) to the stream and the collector will take care of
reclaiming the memory the next time it runs. Simply closing the stream does
not free the buffer (I believe you can still call other api's like GetBuffer
and ToArray after it's closed).
Capacity is a funny beast... you set it while the stream is open and it
can't get smaller than the current length. So if you try to get memory back
with Capacity you'll find that MemoryStream doesn't allow it.
Best to reclaim the space in the usual fashion, simply stop pointing to the
steam and it will go away.

Signature
This posting is provided "AS IS" with no warranties, and confers no rights.
Rico Mariani
CLR Performance Architect
> If you have a MemoryStream, is it beneficial to Close() it after use ?
> Will this reclaim some used memory ?
[quoted text clipped - 7 lines]
> Thanks,
> Stephen