title: Could allocating a byte array be performance-critical?
body: <p>In my small little file transfer website (<a href="https://zeta.li" rel="nofollow noreferrer">this one</a>, running .NET 4.5.1) I'm following the Microsoft Knowledge Base article <a href="https://mskb.pkisolutions.com/kb/812406" rel="nofollow noreferrer">812406</a> to send previously uploaded files from the server to the browser.</p>
  <p>Doing performance optimization I was suprised to find that the line</p>
  <pre><code>var buffer = new byte[10000];
  </code></pre>
  <p>takes a considerable percentage of time (I'm using Red Gate's <a href="https://www.red-gate.com/products/dotnet-development/ants-performance-profiler/" rel="nofollow noreferrer">ANTS Performance Profiler</a>). The buffer is only allocated once per full download/client.</p>
  <p><strong>My questions:</strong></p>
  <ul>
  <li>Is it good practice to allocate a buffer this way and this size?</li>
  <li>Any alternatives on allocating a ≈10k buffer?</li>
  </ul>
  <p><strong>Update 1:</strong></p>
  <p>Thanks to your comments I've seen that the memory is allocated inside the loop, too.</p>
  <p>Still, ANTS Profiler only marks that allocation <em>outside</em> of the loop to take that much time, which I honestly do not understand (yet). I have removed the (meaningless) allocation inside the loop.</p>
  <p><strong>Update 2:</strong></p>
  <p>Having implemented the suggested <a href="https://docs.microsoft.com/en-us/dotnet/api/system.servicemodel.channels.buffermanager" rel="nofollow noreferrer"><code>BufferManager</code></a> and also reduced the buffer size from 10k down to 4096 (just in case...), my website runs very smooth since days.</p>