title: C# System.Buffer.BlockCopy Memory Issue?
body: <p>I have a problem that has been bothering me now for a few days. I have tried to Google the problem,  but so far have not been able to find any solutions, not even a single person with the same problem.</p>
  
  <p>It seems that the C# method System.Buffer.BlockCopy leaves you with some sort of memory ghosts. I have for example this method:</p>
  
  <pre><code>private float[,] readFloatArray2 (byte[] b) {
     int floatSize = sizeof(float);
     float[,] v = new float[2, (b.Length / 2) / floatSize];
     System.Buffer.BlockCopy(b, 0, v, 0, b.Length);
  
     return v;
  }
  </code></pre>
  
  <p>to convert a byte array to a 2D float array. The data is previously read from a stream.
  I have located the problem to be the System.Buffer.BlockCopy method.</p>
  
  <p>If I remove the BlockCopy command, the memory used by the application will be half the size. this means that its not my fault that the byte array is still alive. because without the BlockCopy command, the byte array dies properly. the float array gets created anyway (with or without the copied information).</p>
  
  <p>I am not quite sure if this is a problem of the BlockCopy command or the GC because I have also tried to call System.GC.Collect(); after the BlockCopy and then it also works perfectly (I know you should not do this... thats why I am asking for advise here).</p>
  
  <p>I also wouldn't bother asking, but the issue involves several hundred meg.</p>
  
  <p>Besides the memory issues, the method works perfectly fine. Does anyone know what causes the memory problem?</p>
  
  <p>greetings and thanks in advance 
  oli</p>
  
  <p>ps: I am using .NET4.0 with Visual Studio 2010 PRO and WIN7... don't know whether this is relevant or not.</p>