Microsoft.IO.RecyclableMemoryStream is a high-performance library designed to enhance utility efficiency when working with streams. It’s a substitute for MemoryStream and supplies higher efficiency than MemoryStream cases.
You need to use RecyclableMemoryStream to eradicate LOH (giant object heap) allocations and keep away from reminiscence fragmentation and reminiscence leaks. This text talks concerning the Microsoft.IO.RecyclableMemoryStream library, its goal, and the way it may be utilized in .NET Core functions to spice up utility efficiency.
To work with the code examples supplied on this article, it’s best to have Visible Studio 2019 put in in your system. For those who don’t have already got a duplicate, you’ll be able to download Visual Studio 2019 here.
Create a .NET Core console utility venture in Visible Studio
First off, let’s create a .NET Core console utility venture in Visible Studio. Assuming Visible Studio 2019 is put in in your system, observe the steps outlined under to create a brand new .NET Core console utility venture in Visible Studio.
- Launch the Visible Studio IDE.
- Click on on “Create new venture.”
- Within the “Create new venture” window, choose “Console App (.NET Core)” from the record of templates displayed.
- Click on Subsequent.
- Within the “Configure your new venture” window proven subsequent, specify the title and placement for the brand new venture.
- Click on Create.
This can create a brand new .NET Core console utility venture in Visible Studio 2019. We’ll use this venture to work with Microsoft.IO.RecyclableMemoryStream within the subsequent sections of this text.
RecyclableMemoryStream advantages
Microsoft.IO.RecyclableMemoryStream supplies the next advantages:
- Eliminates LOH allocations utilizing pooled buffers.
- Incurs far fewer technology 2 GCs and spends a lot much less time pausing whereas a GC operation is in progress.
- Avoids reminiscence fragmentation and reminiscence leaks.
- Supplies metrics that can be utilized for monitoring and analyzing efficiency.
How RecyclableMemoryStream works
RecyclableMemoryStream shops the massive buffers used for streams within the technology 2 heap and ensures that these buffers keep there without end. This additionally ensures that full assortment happens occasionally.
The RecyclableMemoryStreamManager class maintains two separate swimming pools:
- Small pool – accommodates small buffers which are utilized in learn/write operations
- Massive pool – accommodates giant buffers used solely when you will have a contiguous buffer
A number of small swimming pools of 128 KB every and enormous swimming pools of 1 MB every (default) are created.
The massive pool has two variations — the linear giant pool and the exponential giant pool. The linear giant pool is the default and grows linearly, and the exponential giant pool grows in an exponential method, i.e., the buffers double in dimension for every slot.
A RecyclableMemoryStream occasion begins by allocating a small buffer initially. Further buffers are chained collectively because the stream capability will increase.
Often, the small pool is used for regular learn/write operations and reminiscence utilization is optimized and environment friendly as a result of the buffers are abstracted from you. That is precisely why RecyclableMemoryStream is far more environment friendly than MemoryStream.
The massive pool is used solely when the appliance is in want of a contiguous reminiscence block.
If you name the GetBuffer technique utilizing the code proven under, the small buffers are transformed to a single, giant, contiguous buffer.
var buffer = recyclableMemoryStreamManager.GetStream().GetBuffer();
Once more, the massive pool might be both of two varieties, linear (default) or exponential.
Though streams are usually not thread-safe themselves, the MemoryManager class is thread-safe.
Set up the RecyclableMemoryStream NuGet bundle
The RecyclableMemoryStream library is accessible as a NuGet bundle. To get began working with the RecyclableMemoryStream library, it’s essential to set up it from NuGet. You possibly can both set up it from the NuGet bundle supervisor or through the use of the next command on the NuGet bundle supervisor console window.
Set up-Package deal Microsoft.IO.RecyclableMemoryStream
Create a reminiscence stream occasion in .NET Core
Assuming that Microsoft.IO.RecyclableMemoryStream has already been put in in your venture, you’ll be able to write the next supply code to put in writing information as a reminiscence stream. Be aware the utilization of the RecyclableMemoryStreamManager class. The GetStream technique of the RecyclableMemoryStreamManager class returns a reminiscence stream occasion.
class Program
personal static readonly RecyclableMemoryStreamManager
recyclableMemoryStreamManager =
new RecyclableMemoryStreamManager();
static void Essential(string[] args)
It ought to be famous right here that we’ve declared the RecyclableMemoryStreamManager occasion simply as soon as. The occasion ought to reside so long as the method is alive, i.e., for the lifetime of the method.
You possibly can optionally present a string tag when calling the GetStream technique. The next code snippet illustrates this.
utilizing (var memoryStream = recyclableMemoryStreamManager.GetStream ("High_Performance_Stream_Demo.Program.Essential"))
Change the parameters of a reminiscence stream pool in .NET Core
It is usually attainable to customise the pool, i.e., change the parameters of the pool when creating an occasion of RecyclableMemoryStreamManager. The next code snippet illustrates how this may be achieved.
int blockSize = 1024;
int largeBufferMultiple = 1024 * 1024;
int maximumBufferSize = 16 * largeBufferMultiple;
int maximumFreeLargePoolBytes = maximumBufferSize * four;
int maximumFreeSmallPoolBytes = 250 * blockSize;
var recyclableMemoryStreamManager = new RecyclableMemoryStreamManager(blockSize, largeBufferMultiple, maximumBufferSize);
recyclableMemoryStreamManager.AggressiveBufferReturn = true;
recyclableMemoryStreamManager.GenerateCallStacks = true;
recyclableMemoryStreamManager.MaximumFreeLargePoolBytes = maximumFreeLargePoolBytes;
recyclableMemoryStreamManager.MaximumFreeSmallPoolBytes = maximumFreeSmallPoolBytes;
RecyclableMemoryStream finest practices
Reminiscence fragmentation can affect the efficiency of your utility, and the giant object heap in .NET is vulnerable to fragmentation. The next tips or finest practices ought to be adhered to when working with RecyclableMemoryStream:
- Set the blockSize, largeBufferMultiple, maxBufferSize, MaximumFreeLargePoolBytes, and MaximumFreeSmallPoolBytes properties to acceptable values.
- Eliminate any stream object as quickly as you’re executed utilizing it.
- By no means name the ToArray technique.
- Keep away from calling the GetBuffer technique.
Microsoft.IO.RecyclableMemoryStream is a pooled reminiscence stream allocator that’s adept at decreasing GC load and bettering the efficiency of your functions. Microsoft.IO.RecyclableMemoryStream takes benefit of pooled buffers to eradicate giant object heap (LOH) allocations. It not solely avoids reminiscence fragmentation and reminiscence leaks but in addition supplies metrics that can be utilized for monitoring efficiency.