Here’s the first version of my code using Microsoft.IO.RecyclableMemoryStream

I mentioned a while back that I was going to do this…

The app is heavy with its usage of streams, so replacing all references to MemoryStream with one that reuses pooled buffers seemed like a logical thing to do. I already use an object pool for thumbnail control instances (the most used object in the application), so it seems like an obvious way to improve performance and possibly memory management too. And it works well.

If you are interested in reading about RecyclableMemoryStream, read their announcement post.

If you missed my post describing what my code does, you can read it here. The code is still shared in the same place on Google drive here.

As for the code, usage is quite straightforward… You create an instance of their memory stream manager, and instead of creating MemoryStream instances as you normally would, use its GetStream method. That’s about it… The only thing that changed for me is when using such streams as read-only buffers (the MemoryStream(Byte[]) constructor) because this package doesn’t have a GetStream() method with identical signature. They do have an equivalent one though… it just takes a couple of extra parameters, such as tag, offset, and length. My implementation of the code changes in my solution simply used a Singleton for the memory stream manager, and then I had to replace all occurrences of “new MemoryStream…) in the code, which was slightly tedious because of the different signature for read-only mode streams.

About Jerome

I am a senior C# developer in Johannesburg, South Africa. I am also a recovering addict, who spent nearly eight years using methamphetamine. I write on my recovery blog about my lessons learned and sometimes give advice to others who have made similar mistakes, often from my viewpoint as an atheist, and I also write some C# programming articles on my programming blog.
This entry was posted in Programming and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s