title: Memorystream and Large Object Heap
body: <p>I have to transfer large files between computers on via unreliable connections using WCF. </p>
  
  <p>Because I want to be able to resume the file and I don't want to be limited in my filesize by WCF, I am chunking the files into 1MB pieces. These "chunk" are transported as stream. Which works quite nice, so far.</p>
  
  <p>My steps are:</p>
  
  <ol>
  <li>open filestream</li>
  <li>read chunk from file into byte[] and create memorystream </li>
  <li>transfer chunk</li>
  <li>back to 2. until the whole file is sent</li>
  </ol>
  
  <p>My problem is in step 2. I assume that when I create a memory stream from a byte array, it will end up on the LOH and ultimately cause an outofmemory exception. I could not actually create this error, maybe I am wrong in my assumption.</p>
  
  <p>Now, I don't want to send the byte[] in the message, as WCF will tell me the array size is too big. I can change the max allowed array size and/or the size of my chunk, but I hope there is another solution.</p>
  
  <p><strong>My actual question(s):</strong></p>
  
  <ul>
  <li>Will my current solution create objects on the LOH and will that cause me problem? </li>
  <li>Is there a better way to solve this?</li>
  </ul>
  
  <p>Btw.: On the receiving side I simple read smaller chunks from the arriving stream and write them directly into the file, so no large byte arrays involved.</p>
  
  <p>Edit:</p>
  
  <p>current solution:</p>
  
  <pre><code>for (int i = resumeChunk; i &lt; chunks; i++)
  {
   byte[] buffer = new byte[chunkSize];
   fileStream.Position = i * chunkSize;
   int actualLength = fileStream.Read(buffer, 0, (int)chunkSize);
   Array.Resize(ref buffer, actualLength);
   using (MemoryStream stream = new MemoryStream(buffer)) 
   {
    UploadFile(stream);
   }
  }
  </code></pre>