title: Heap fragmentation when using byte arrays
body: <p>I have a C# 4.0 application (single producer/single consumer) which transfers huge amount of data in chunks. Although there's no new memory allocation I run out of memory after a while.</p>
  
  <p>I profiled memory using Redgate memory profiler and there are a lot of free memory there. It says free memory cannot be used because of fragmentation.</p>
  
  <p>I use a blocking collection as the buffer and byte arrays as the members:</p>
  
  <pre><code>BlockingCollection&lt;byte[]&gt; segments = new BlockingCollection&lt;byte[]&gt;(8);
  // producer:
  segments.Add(buffer);
  // consumer:
  byte[] buffer = _segments.Take();
  </code></pre>
  
  <p>How can I avoid managed memory fragmentation?</p>