BT

Pooled Memory Streams for .NET

| by Jonathan Allen Follow 636 Followers on Mar 09, 2015. Estimated reading time: 1 minute |

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.

Rate this Article

Adoption Stage
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread
Community comments

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Discuss
BT