Like most languages that rely on a mark-and-sweep garbage collector, C# can run into performance problems when allocating memory too often or when making large allocations. Ben Watson, a Senior SDE at Microsoft working on Bing, ran into just that problem with the MemoryStream class. He in his book, Writing High-Performance .NET Code, he wrote,
In one application that suffered from too many LOH allocations, we discovered that if we pooled a single type of object, we could eliminate 99% of all problems with the LOH. This was MemoryStream, which we used for serialization and transmitting bits over the network. The actual implementation is more complex than just keeping a queue of MemoryStream objects because of the need to avoid fragmentation, but conceptually, that is exactly what it is. Every time a MemoryStream object was disposed, it was put back in the pool for reuse.
Since then Microsoft has decided to release the code he referred to in a class called Microsoft.IO.RecycableMemoryStream. This class is a drop-in replacement for traditional memory streams. In the simplest case, you simple write “manager.GetStream” in place of “new MemoryStream”.
Note that the stream managers are thread safe and should be used for the life of the process.