title: OutOfMemoryException when send big file 500MB using FileStream ASPNET
body: <p>I'm using Filestream for read big file (> 500 MB) and I get the OutOfMemoryException. </p>
  
  <p>I use Asp.net , .net 3.5, win2003, iis 6.0</p>
  
  <p>I want this in my app:</p>
  
  <p>Read DATA from Oracle</p>
  
  <p>Uncompress file using FileStream and BZip2</p>
  
  <p>Read file  uncompressed and send it to asp.net page for download. </p>
  
  <p>When I read file from disk, Fails !!! and get OutOfMemory...</p>
  
  <p>. My Code is:</p>
  
  <pre><code>using (var fs3 = new FileStream(filePath2, FileMode.Open, FileAccess.Read)) 
          { 
            byte[] b2 = ReadFully(fs3, 1024); 
          } 
  
   // http://www.yoda.arachsys.com/csharp/readbinary.html
   public static byte[] ReadFully(Stream stream, int initialLength) 
    { 
      // If we've been passed an unhelpful initial length, just 
      // use 32K. 
      if (initialLength &lt; 1) 
      { 
        initialLength = 32768; 
      } 
  
      byte[] buffer = new byte[initialLength]; 
      int read = 0; 
  
      int chunk; 
      while ((chunk = stream.Read(buffer, read, buffer.Length - read)) &gt; 0) 
      { 
        read += chunk; 
  
        // If we've reached the end of our buffer, check to see if there's 
        // any more information 
        if (read == buffer.Length) 
        { 
          int nextByte = stream.ReadByte(); 
  
          // End of stream? If so, we're done 
          if (nextByte == -1) 
          { 
            return buffer; 
          } 
  
          // Nope. Resize the buffer, put in the byte we've just 
          // read, and continue 
          byte[] newBuffer = new byte[buffer.Length * 2]; 
          Array.Copy(buffer, newBuffer, buffer.Length); 
          newBuffer[read] = (byte)nextByte; 
          buffer = newBuffer; 
          read++; 
        } 
      } 
      // Buffer is now too big. Shrink it. 
      byte[] ret = new byte[read]; 
      Array.Copy(buffer, ret, read); 
      return ret; 
    } 
  </code></pre>
  
  <p>Now, I specify my issue better.</p>
  
  <p>Uncompress file using FileStream and BZip2 is OK, all is right.</p>
  
  <p>The Problem is the following:</p>
  
  <p>Read fat big file in disk (> 500 MB) in byte[] and send bytes to Response (asp.net) for download it.</p>
  
  <p>When use</p>
  
  <p><a href="http://www.yoda.arachsys.com/csharp/readbinary.html" rel="nofollow noreferrer">http://www.yoda.arachsys.com/csharp/readbinary.html</a></p>
  
  <pre><code>public static byte[] ReadFully
  </code></pre>
  
  <p>I get the error:  OutOfMemoryException...</p>
  
  <p>If better BufferedStream than Stream (FileStream, MemoryStream, ...) ??</p>
  
  <p>Using BufferedStream , Can I read big file of 700 MB ?? (any sample code source using BufferedStream for download big file)</p>
  
  <p>I think, this is the question: Not "how to read a 500mb file into memory?" , But "how to send a large file to the ASPNET Response stream?"</p>
  
  <p>I found this code by Cheeso:</p>
  
  <pre><code>using (var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))  
  {  
     Response.BufferOutput= false;   // to prevent buffering 
     byte[] buffer = new byte[1024]; 
     int bytesRead = 0; 
     while ((bytesRead = fs.Read(buffer, 0, buffer.Length)) &gt; 0)  
     { 
         Response.OutputStream.Write(buffer, 0, bytesRead); 
     } 
  }
  </code></pre>
  
  <p>Is it good code ?? any improvements for high performance  ??</p>
  
  <p>A collegue say me, use</p>
  
  <pre><code>Response.TransmitFile(filePath);
  </code></pre>
  
  <p>Now, another question, better TransmitFile or code by Cheeso ??</p>
  
  <p>Many years ago, in msdn magazine appears great article about it but I cannot access <a href="http://msdn.microsoft.com/msdnmag/issues/06/09/WebDownloads/" rel="nofollow noreferrer">http://msdn.microsoft.com/msdnmag/issues/06/09/WebDownloads/</a>,</p>
  
  <p><strong>Update</strong>: You can access using <strong>webarchive</strong> in the link: <a href="https://web.archive.org/web/20070627063111/http://msdn.microsoft.com/msdnmag/issues/06/09/WebDownloads/" rel="nofollow noreferrer">https://web.archive.org/web/20070627063111/http://msdn.microsoft.com/msdnmag/issues/06/09/WebDownloads/</a></p>
  
  <p>Any suggestions, comments, sample code source??</p>