Home > .NET, Exploring the CodeFluent Runtime > Exploring the CodeFluent Runtime: ChunkedMemoryStream

Exploring the CodeFluent Runtime: ChunkedMemoryStream


 
Today, on the series “Exploring the CodeFluent Runtime” we are going to take a look at the ChunkedMemoryStream class on the CodeFluent.Runtime.Utilities namespace.
 
This class allows you to manipulate a memory stream by “chunks”. This can be useful when you are dealing with memory streams big enough (85,000 bytes) to be allocated on the Large Object Heap (LOH).
 
The ChunkedMemoryStream can be used instead of the System.IO.MemoryStream (mscorlib.dll) to avoid memory fragmentation.
 
Let’s build a simple example in order to compare the use of a System.IO.MemoryStream against the CodeFluent.Runtime.Utilities.ChunkedMemoryStream.
 
Let’s say I want to load a file’s content in memory (big enough to sit on the LOH).
 

static void Main(string[] args)
{
  FileStream fileSm = new FileStream(@"file_path", FileMode.Open);
  Stream ms = new MemoryStream();
  fileSm.CopyTo(ms);
  ms.Close();
  fileSm.Close();

  System.Diagnostics.Debugger.Break();
}

 
When the Debugger breaks let’s explore the LOH:
 

LOH using MemoryStream

LOH using MemoryStream


 
We can see that the LOH size is (17184 + 41943056 + 83886096 = 125846336 bytes) and we easily distinguish a memory allocation for 2 byte arrays (perhaps some buffers used internally when copying the data from one stream to another).
 
Let’s now change only one line in our code example and use the CodeFluent.Runtime.Utilities.ChunkedMemoryStream.
 

static void Main(string[] args)
{
  FileStream fileSm = new FileStream(@"file_path", FileMode.Open);
  Stream ms = new CodeFluent.Runtime.Utilities.ChunkedMemoryStream();
  fileSm.CopyTo(ms);
  ms.Close();
  fileSm.Close();

  System.Diagnostics.Debugger.Break();
}

 
And let’s explore the LOH:
 

LOH using ChunkedMemoryStream

LOH using ChunkedMemoryStream


 
We now see that the LOH size is 17184 bytes and we cannot see any trace of a byte array.
 

Using the ChunkedMemoryStream we avoid allocating memory on the LOH when manipulating big streams.

 
Remeber that the CodeFluent Runtime Client is a free library that provides very useful and powerful helpers like:

  • XML utilities
  • IO utilities
  • Type conversion helpers
  • JSON utilities
  • … and many other

 
You can easily install the CodeFluent Runtime Client from Nugget.
 
PM> Install-package CodeFluentRuntimeClient
 
 
Regards,
 
Pablo Fernadez Duran
 
 

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s